sendKeys() 不使用 WebDriverWait 通过 Selenium 和 Java 插入完整值



sendKeys() 没有将完整的字符串插入到文本字段中。我正在尝试插入电子邮件 ID。

String name = "New Apollo33";
fluent_wait.until(ExpectedConditions.presenceOfElementLocated(By.id("businessname"))).sendKeys(name);
String email = "apollo33@mailinator.com";
fluent_wait.until(ExpectedConditions.presenceOfElementLocated(By.id("businessemail"))).sendKeys(email);

它插入了名称,但没有完全插入电子邮件 ID。

我在输入预先格式化的电话号码字段时自动执行其中一个测试用例时遇到了类似的问题。以下是我为确保脚本能够在文本框中输入文本所做的工作:

  • 手动检查文本框是否接受脚本尝试输入的文本长度。检查两次:)永远不会有什么坏处。
  • 使用 sendKey 输入值,并从 DOM 获取元素的值。
  • 虽然 DOM 中文本框元素的值不等于您尝试输入的文本,但请重试。
  • 确保在 while 循环中设置退出条件,以便有办法退出测试。

您的实现可以是这样的:

Wait<WebDriver> fluent_wait = new FluentWait<WebDriver>(driver)
.withTimeout(60, SECONDS)
.pollingEvery(2, SECONDS) 
.ignoring(NoSuchElementException.class);
WebElement emailElement= fluent_wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("businessemail"));
}
});
String emailText = "apollo33@mailinator.com";
long startTime = System.currentTimeMillis(); // This is to run the while loop for a specified amount of time and use it as an exit condition for the while loop.
//the below condition assumes that the text box sets some kind of attribute in the DOM element once the user enters the value in the text box.
while(!emailElement.getAttribute("value").equals(emailText)&&System.currentTimeMillis() - startTime) < 2000){
emailElement.clear();
emailElement.sendKeys(emailText);
}
//if the above doesn't work, add the below as a fall back:
if(!emailElement.getAttribute("value").equals(emailText)){
emailElement.clear();
for(char ch: emailText.toCharArray()){
emailElement.sendKeys(String.valueOf(ch));
try{
Thread.sleep(15); //making the thread sleep for 15 milliseconds, taking a performance hit to make the sendKeys work.
}catch(InterruptedException e){
}
}
}

仅当 while 循环无法在 2 秒内设置文本框中的文本时,才会执行 Thread.sleep 的回退条件。如果 2 秒/2000 毫秒不够,您可以增加时间。thread.sleep 在每次字符迭代之间命中 15 毫秒。我们将其整合到我们的框架中,以涵盖使用不同前端技术开发的各种文本框。这对我们作为一个组织来说效果很好,所以希望它也能为你工作。

上面的关键是不要被selenium提供的预定义实用程序所困扰,相反,你会想要使用Java提供的DOM值和选项。希望您能够尽快找到解决问题的方法。 祝你好运!

作为经验法则,在尝试发送字符序列而不是调用时presenceOfElementLocated()始终调用elementToBeClickable(),您可以使用以下解决方案:

fluent_wait.until(ExpectedConditions.elementToBeClickable(By.id("businessname"))).sendKeys("New Apollo33");
fluent_wait.until(ExpectedConditions.elementToBeClickable(By.id("businessemail"))).sendKeys("apollo33@mailinator.com");

我遇到了同样的问题。sendKeys-Keys.chord没有插入完整的值(速度太快,浏览器甚至没有时间"有时"写它)

之前:(不能 100% 工作)

action.sendKeys(Keys.chord("string")).perform();

在编写"字符串"时,这太快了......有时(例如从 1/5 到 1/10 次)它在我发送到的网站上看起来不完整。这非常令人沮丧。

我尝试应用的解决方案为我工作了几天,但最终发生了同样的事情(1/20 但失败了)我尝试的解决方案只是添加一个 在sendKeys-Keys.chord之后的1.5seg的"线程睡眠"(也许网站需要更多时间来编写它):

之后:(不能 100% 工作 - 我也尝试了这个可能的解决方案,它只是失败了,但仍然失败了 1/20)

action.sendKeys(Keys.chord("string")).perform();
Thread.sleep(1500);
<小时 />

最终解决方案:(100%)

添加一个 do-while 将比较/检查 sendKeys-Keys.chord 是否与浏览器 (WebDriver ) 上实际写入的内容匹配,如果不匹配,它将再次写入(您现在可以减少 Thread.sleep 的延迟)

WebDriverWait wait = new WebDriverWait(driver,40);
Actions action = new Actions(driver);
String TextSentenceORWordToIntroduce = "Dinosaur";
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath_to_element"))).click();
Thread.sleep(200);
action.sendKeys(Keys.chord(TextSentenceORWordToIntroduce)).perform();
Thread.sleep(500);
String comprovation = driver.findElement(By.xpath("xpath_to_element")).getAttribute("value"); // or rarely ")).getText();"
if (!comprovation.contentEquals(TextSentenceORWordToIntroduce)) { // if not then ALL WORKS FINE ;) and won't enter the Do-while
driver.findElement(By.xpath("xpath_to_element")).click();
do {
Thread.sleep(200);
action.sendKeys(Keys.HOME).perform();
Thread.sleep(200);
action.keyDown(Keys.SHIFT).perform();
Thread.sleep(200);
action.sendKeys(Keys.END).perform();
Thread.sleep(200);
action.keyUp(Keys.SHIFT).perform();
Thread.sleep(200);
action.sendKeys(Keys.chord(TextSentenceORWordToIntroduce)).perform();
Thread.sleep(500);
comprovation = driver.findElement(By.xpath("xpath_to_element")).getAttribute("value"); // or rarely ")).getText();"
} while (!comprovation.contentEquals(TextSentenceORWordToIntroduce));
}

希望此解决方案可以帮助遇到相同问题的人^^

干杯!

我遇到了类似的问题,其中将密钥作为单个字符串发送将仅使用以下方法输入单个字符:

public void enterText(WebElement element, String text){
element.clear();
element.sendKeys(text);
}

我的解决方案是将字符串转换为字符数组,并分别输入每个字符,如下所示:

public void enterTextCharByChar(WebElement element, String charSeq){
char[] inputText = charSeq.toCharArray();
// clear any pre-existing text
element.clear();
for (char val : inputText) {
element.sendKeys(String.valueOf(val));
}
}

希望这有帮助。

我经常发现sendKeys()移动太快或输入不完整的字符串。 您需要以非常小的延迟将其包装在 while 循环中:

while(element.text != input) {
element.sendKeys(Keys.chord(Keys.CONTROL, "a"), input); //or whatever
Thread.sleep(100) //100ms not 100sec
}

我知道thread.sleep非常不受欢迎,但是waitFor()需要同样长的时间

element.clear()

也可以工作,但使用 clear() 或 Ctrl+a + 输入,因为使用两者都是多余的

最新更新