我如何始终使用Selenium从输入元素中删除默认文本? [英] How can I consistently remove the default text from an input element with Selenium?

查看:2033
本文介绍了我如何始终使用Selenium从输入元素中删除默认文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Selenium WebDriver将文本输入到具有默认文本输入用户ID的GWT输入元素。以下是我试图使其发挥作用的几种方法:

  searchField.click(); 
if(!searchField.getAttribute(value)。isEmpty()){
//清除字段,如果不是空的
searchField.clear();

if(!searchField.getAttribute(value)。isEmpty()){
//如果它仍然没有清除,请点击并点击
externalLinksHeader 。点击();
searchField.click();
}

searchField.sendKeys(username);

奇怪的是上面这个只适用于某些时间。有时,它最终会搜索输入用户IDus,基本上会在默认文本后面输入用户名 - 甚至没有完成。



其他更好,更可靠的方法来清除GWT元素的默认文本?



编辑添加:输入元素的HTML。不幸的是,由于JS / GWT的热度,没有什么可看的。以下是未选中的字段:

 < input type =textclass =gwt-TextBox emptymaxlength = 40\" > 

在点击它并手动赋予焦点后,默认文本和空类

< setDefaultText()被调用 onBlur() onChange(),如果更改结果为空的文本字段。猜猜这就是为什么 searchField.clear()没有帮助。



我也通过这种方法在调试模式下,在这种情况下,它永远不会工作。正常运行时,大部分时间都能正常工作。我不能说为什么,但是。

解决方案

好的,当明确()方法将清除输入并将其保留为空。它提出的解决方案如下。


  1. 天真的一个,按 Backspace 10次:

      String b = Keys.BACK_SPACE.toString(); 
    searchField.sendKeys(b + b + b + b + b + b + b + b + b + b + username);

    StringUtils.repeat() Strings.repeat() 可能会派上用场)


  2. 使用 Ctrl + A Delete

      String del = Keys.chord(Keys.CONTROL,a)+ Keys.DELETE; 
    searchField.sendKeys(del + username);


  3. 删除输入内容通过JavaScript:

      JavascriptExecutor js =(JavascriptExecutor)驱动程序; 
    js.executeScript(arguments [0] .value ='';,searchField);
    searchField.sendKeys(username);


  4. 设置输入值通过JavaScript完全:

      JavascriptExecutor js =(JavascriptExecutor)驱动程序; 
    js.executeScript(arguments [0] .value ='+ username +';,searchField);


请注意,javascript可能无法正常工作如下所示:为什么我无法使用javascript清除输入字段?


I'm trying to use Selenium WebDriver to input text to a GWT input element that has default text, "Enter User ID". Here are a few ways I've tried to get this to work:

        searchField.click();
        if(!searchField.getAttribute("value").isEmpty()) {
            // clear field, if not already empty 
            searchField.clear();
        }
        if(!searchField.getAttribute("value").isEmpty()) {
            // if it still didn't clear, click away and click back
            externalLinksHeader.click();
            searchField.click();
        }

        searchField.sendKeys(username);

The strange thing is the above this only works some of the time. Sometimes, it ends up searching for "Enter User IDus", basically beginning to type "username" after the default text -- and not even finishing that.

Any other better, more reliable ways to clear out default text from a GWT element?

Edited to add: The HTML of the input element. Unfortunately, there's not much to see, thanks to the JS/GWT hotness. Here's the field when it's unselected:

<input type="text" class="gwt-TextBox empty" maxlength="40">

After I've clicked it and given it focus manually, the default text and the "empty" class are removed.

The JS to setDefaultText() gets called both onBlur() and onChange() if the change results in an empty text field. Guess that's why the searchField.clear() isn't helping.

I've also stepped through this method in debug mode, and in that case, it never works. When run normally, it works the majority of the time. I can't say why, though.

解决方案

Okay, the script obviously kicks in when the clear() method clears the input and leaves it empty. The solutions it came up with are given below.

  1. The naïve one, presses Backspace 10 times:

    String b = Keys.BACK_SPACE.toString();
    searchField.sendKeys(b+b+b+b+b+b+b+b+b+b + username);
    

    (StringUtils.repeat() from Apache Commons Lang or Google Guava's Strings.repeat() may come in handy)

  2. The nicer one using Ctrl+A, Delete:

    String del = Keys.chord(Keys.CONTROL, "a") + Keys.DELETE; 
    searchField.sendKeys(del + username);
    

  3. Deleting the content of the input via JavaScript:

    JavascriptExecutor js = (JavascriptExecutor)driver;
    js.executeScript("arguments[0].value = '';", searchField);
    searchField.sendKeys(username);
    

  4. Setting the value of the input via JavaScript altogether:

    JavascriptExecutor js = (JavascriptExecutor)driver;
    js.executeScript("arguments[0].value = '" + username + "';", searchField);
    

Note that javascript might not always work, as shown here: Why can't I clear an input field with javascript?

这篇关于我如何始终使用Selenium从输入元素中删除默认文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆