如何始终使用Selenium从输入元素中删除默认文本?

时间:2022-03-05 07:39:17

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:

我正在尝试使用Selenium WebDriver将文本输入到具有默认文本的GWT输入元素“输入用户ID”。以下是我尝试过的一些方法:

        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.

奇怪的是上面的这只在某些时候起作用。有时,它会搜索“Enter User IDus”,基本上是在默认文本之后输入“username”——甚至不会完成这个操作。

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

还有什么更好、更可靠的方法可以从GWT元素中清除默认文本吗?

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:

编辑为添加:输入元素的HTML。不幸的是,由于JS/GWT很热,没有什么可看的。这是未经选择的领域:

<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.

如果更改导致文本字段为空,则setDefaultText()的JS将同时调用onBlur()和onChange()。我猜这就是为什么searchfiel. clear()不起作用。

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.

我还在调试模式中使用了这个方法,在这种情况下,它永远不会工作。当正常运行时,它大部分时间都在工作。但我说不出为什么。

4 个解决方案

#1


18  

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.

好的,当clear()方法清除输入并使其为空时,脚本显然会启动。它提出的解决方案如下。

  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)

    (StringUtils.repeat()来自Apache Commons Lang或谷歌Guava的string .repeat()可能会派上用场)

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

    使用Ctrl+A,删除:

    String del = Keys.chord(Keys.CONTROL, "a") + Keys.DELETE; 
    searchField.sendKeys(del + username);
    
  3. Deleting the content of the input via JavaScript:

    通过JavaScript删除输入内容:

    JavascriptExecutor js = (JavascriptExecutor)driver;
    js.executeScript("arguments[0].value = '';", searchField);
    searchField.sendKeys(username);
    
  4. Setting the value of the input via JavaScript altogether:

    通过JavaScript设置输入的值:

    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?

注意,javascript可能并不总是有效,如这里所示:为什么我不能使用javascript清除输入字段?

#2


0  

For what it is worth I'm have a very similar issue. WebDriver 2.28.0 and FireFox 18.0.1

无论如何,我有一个非常相似的问题。WebDriver 2.28.0和FireFox 18.0.1

I'm also using GWT but can reproduce it with simple HTML/JS:

我也在使用GWT,但是可以用简单的HTML/JS复制它:

<html>
<body>
<div>
<h3>Box one</h3>
<input id="boxOne" type="text" onfocus="if (this.value == 'foo') this.value = '';" onblur="if (this.value == '') this.value = 'foo';"/>
</div>
<div>
<h3>Box two</h3>
<input id="boxTwo" type="text" />
</div>
</body>
</html>

This test fails most of the time:

这个测试大多数时候都失败了:

@Test
public void testTextFocusBlurDirect() throws Exception {
  FirefoxDriver driver = new FirefoxDriver();

  driver.navigate().to(getClass().getResource("/TestTextFocusBlur.html"));

  for (int i = 0; i < 200; i++) {
      String magic = "test" + System.currentTimeMillis();
      driver.findElementById("boxOne").clear();
      Thread.sleep(100);
      driver.findElementById("boxOne").sendKeys(magic);
      Thread.sleep(100);
      driver.findElementById("boxTwo").clear();
      Thread.sleep(100);
      driver.findElementById("boxTwo").sendKeys("" + i);
      Thread.sleep(100);
      assertEquals(magic, driver.findElementById("boxOne").getAttribute("value"));
  }

  driver.quit();
}

It could just be the OS taking focus away from the browser in a way WebDriver can't control. We don't seem to get this issue on the CI server to maybe that is the case.

它可能只是操作系统将注意力从浏览器上转移开,而WebDriver无法控制。我们似乎没有在CI服务器上发现这个问题,也许是这样。

#3


0  

I cannot add a comment yet, so I am putting it as an answer here. I want to inform you that if you want to use only javascript to clear and/or edit an input text field, then the javascript approach given by @slanec will not work. Here is an example: Why can't I clear an input field with javascript?

我还不能添加评论,所以我把它作为一个答案。我想告诉您,如果您只想使用javascript来清除和/或编辑输入文本字段,那么@slanec给出的javascript方法将不会起作用。这里有一个例子:为什么我不能用javascript清除输入字段?

#4


0  

In case you use c# then solution would be :

// provide some text
webElement.SendKeys("aa");
// this is how you use this in C# , VS
String b = Keys.Backspace.ToString();

// then provide back space few times 
webElement.SendKeys(b + b + b + b + b + b + b + b + b + b);

#1


18  

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.

好的,当clear()方法清除输入并使其为空时,脚本显然会启动。它提出的解决方案如下。

  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)

    (StringUtils.repeat()来自Apache Commons Lang或谷歌Guava的string .repeat()可能会派上用场)

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

    使用Ctrl+A,删除:

    String del = Keys.chord(Keys.CONTROL, "a") + Keys.DELETE; 
    searchField.sendKeys(del + username);
    
  3. Deleting the content of the input via JavaScript:

    通过JavaScript删除输入内容:

    JavascriptExecutor js = (JavascriptExecutor)driver;
    js.executeScript("arguments[0].value = '';", searchField);
    searchField.sendKeys(username);
    
  4. Setting the value of the input via JavaScript altogether:

    通过JavaScript设置输入的值:

    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?

注意,javascript可能并不总是有效,如这里所示:为什么我不能使用javascript清除输入字段?

#2


0  

For what it is worth I'm have a very similar issue. WebDriver 2.28.0 and FireFox 18.0.1

无论如何,我有一个非常相似的问题。WebDriver 2.28.0和FireFox 18.0.1

I'm also using GWT but can reproduce it with simple HTML/JS:

我也在使用GWT,但是可以用简单的HTML/JS复制它:

<html>
<body>
<div>
<h3>Box one</h3>
<input id="boxOne" type="text" onfocus="if (this.value == 'foo') this.value = '';" onblur="if (this.value == '') this.value = 'foo';"/>
</div>
<div>
<h3>Box two</h3>
<input id="boxTwo" type="text" />
</div>
</body>
</html>

This test fails most of the time:

这个测试大多数时候都失败了:

@Test
public void testTextFocusBlurDirect() throws Exception {
  FirefoxDriver driver = new FirefoxDriver();

  driver.navigate().to(getClass().getResource("/TestTextFocusBlur.html"));

  for (int i = 0; i < 200; i++) {
      String magic = "test" + System.currentTimeMillis();
      driver.findElementById("boxOne").clear();
      Thread.sleep(100);
      driver.findElementById("boxOne").sendKeys(magic);
      Thread.sleep(100);
      driver.findElementById("boxTwo").clear();
      Thread.sleep(100);
      driver.findElementById("boxTwo").sendKeys("" + i);
      Thread.sleep(100);
      assertEquals(magic, driver.findElementById("boxOne").getAttribute("value"));
  }

  driver.quit();
}

It could just be the OS taking focus away from the browser in a way WebDriver can't control. We don't seem to get this issue on the CI server to maybe that is the case.

它可能只是操作系统将注意力从浏览器上转移开,而WebDriver无法控制。我们似乎没有在CI服务器上发现这个问题,也许是这样。

#3


0  

I cannot add a comment yet, so I am putting it as an answer here. I want to inform you that if you want to use only javascript to clear and/or edit an input text field, then the javascript approach given by @slanec will not work. Here is an example: Why can't I clear an input field with javascript?

我还不能添加评论,所以我把它作为一个答案。我想告诉您,如果您只想使用javascript来清除和/或编辑输入文本字段,那么@slanec给出的javascript方法将不会起作用。这里有一个例子:为什么我不能用javascript清除输入字段?

#4


0  

In case you use c# then solution would be :

// provide some text
webElement.SendKeys("aa");
// this is how you use this in C# , VS
String b = Keys.Backspace.ToString();

// then provide back space few times 
webElement.SendKeys(b + b + b + b + b + b + b + b + b + b);