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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11251916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:24:19  来源:igfitidea点击:

How can I consistently remove the default text from an input element with Selenium?

javagwtseleniumwebdriver

提问by mcole

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 将文本输入到具有默认文本“输入用户 ID”的 GWT 输入元素。以下是我尝试使用的几种方法:

        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.

JS tosetDefaultText()被调用onBlur()onChange()如果更改导致空文本字段。猜猜这就是为什么searchField.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.

我也在调试模式下逐步执行了此方法,在这种情况下,它永远不会起作用。正常运行时,它大部分时间都在工作。不过,我不能说为什么。

回答by Petr Jane?ek

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

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

  1. The na?ve one, presses Backspace10 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 inputvia JavaScript:

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

    JavascriptExecutor js = (JavascriptExecutor)driver;
    js.executeScript("arguments[0].value = '" + username + "';", searchField);
    
  1. 天真一,按Backspace10次​​:

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

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

  2. 更好的使用Ctrl+ A,Delete

    String del = Keys.chord(Keys.CONTROL, "a") + Keys.DELETE; 
    searchField.sendKeys(del + username);
    
  3. 删除inputvia JavaScript的内容:

    JavascriptExecutor js = (JavascriptExecutor)driver;
    js.executeScript("arguments[0].value = '';", searchField);
    searchField.sendKeys(username);
    
  4. 完全设置inputvia 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 清除输入字段?

回答by salk31

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 服务器上解决这个问题,也许就是这种情况。

回答by MasterJoe2

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 @slanecwill not work. Here is an example: Why can't I clear an input field with javascript?

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

回答by Mike ASP

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);