sendKeys() 未使用网络驱动程序通过硒和 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。


白衣染霜花
浏览 120回答 3
3回答

人到中年有点甜

我有同样的问题。发送键-键.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%有效)添加一个操作时,它将进行编译/检查发送键键.chord是否与浏览器上实际写入的内容匹配(WebDriver),如果它不匹配,它将再次写入它(您现在可以减少线程睡眠的延迟)。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));}希望这个解决方案可以帮助有同样问题的人^^

开满天机

在输入预先格式化的电话号码字段时自动执行一个测试用例时,我遇到了类似的问题。以下是我为确保脚本能够在文本框中输入文本所做的工作:手动检查文本框是否接受脚本尝试输入的文本长度。检查两次:)永远不会有什么坏处。使用发送键输入值,并从 DOM 中获取元素的值。虽然 DOM 中文本框元素的值不等于您尝试输入的文本,请重试。确保在 while 循环中设置退出条件,以便有办法退出测试。您的实现可以是这样的:Wait<WebDriver> fluent_wait = new FluentWait<WebDriver>(driver)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.withTimeout(60, SECONDS)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.pollingEvery(2, SECONDS)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.ignoring(NoSuchElementException.class);WebElement emailElement= fluent_wait.until(new Function<WebDriver, WebElement>() {&nbsp; &nbsp; &nbsp;public WebElement apply(WebDriver driver) {&nbsp; &nbsp; &nbsp;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){&nbsp; &nbsp; &nbsp;emailElement.clear();&nbsp; &nbsp; &nbsp;emailElement.sendKeys(emailText);}//if the above doesn't work, add the below as a fall back:if(!emailElement.getAttribute("value").equals(emailText)){&nbsp; &nbsp; emailElement.clear();&nbsp; &nbsp; for(char ch: emailText.toCharArray()){&nbsp; &nbsp; &nbsp; &nbsp; emailElement.sendKeys(String.valueOf(ch));&nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Thread.sleep(15); //making the thread sleep for 15 milliseconds, taking a performance hit to make the sendKeys work.&nbsp; &nbsp; &nbsp; &nbsp; }catch(InterruptedException e){&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}仅当 while 循环无法将文本框中的文本设置为 2 秒时,才会执行 Thread.sleep 的回退条件。如果 2 秒/2000 毫秒不够,您可以增加时间。线程睡眠在每次字符迭代之间命中的时间仅为 15 毫秒。我们将其合并到我们的框架中,以涵盖以不同前端技术开发的各种文本框。这对我们作为一个组织来说很好,所以希望它也能为你工作。上面的关键是不要陷入硒提供的预定义实用程序,而是要使用Java提供的DOM值和选项。希望您能够尽快找到解决问题的方法。祝你好运!

SMILET

作为尝试发送字符序列而不是调用始终调用元素 ToBeCable()&nbsp;时的拇指规则,您可以使用以下解决方案:presenceOfElementLocated()fluent_wait.until(ExpectedConditions.elementToBeClickable(By.id("businessname"))).sendKeys("New&nbsp;Apollo33"); fluent_wait.until(ExpectedConditions.elementToBeClickable(By.id("businessemail"))).sendKeys("apollo33@mailinator.com");
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java