【问题标题】:Wait before sendkeys in Selenium Java在 Selenium Java 中的 sendkeys 之前等待
【发布时间】:2021-02-05 17:50:30
【问题描述】:

我正在使用 JavaSelenium 和 chrome 进行测试自动化。 我想输入一段文字,等它显示出来然后点击TAB,我想避开thread.sleep 所以我正在使用这段代码:

        WebElement societe = wait.until(ExpectedConditions.elementToBeClickable(By.id("AutoComplInputBoxfld_XSociete")));
        societe.sendKeys("Text");
        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
        societe.sendKeys(Keys.TAB);

但它将字段留空并转到下一个元素。

  1. 我该如何解决?
  2. 有没有办法在发送键之前使用显式等待?

【问题讨论】:

  • 不要混合隐式和显式等待。 (坚持你的 wait.until 等待位......)
  • 我使用了隐式等待来等待文本显示,所以我可以点击 TAB 键
  • 是的...不要那样做。你不应该混合隐式和显式等待。将为未来的所有驱动程序操作设置隐式等待......因此它们可以并且将与使用相同类型的轮询机制并忽略的显式等待冲突......
  • 好的,我会删除它。谢谢解释
  • 很多人把它误认为是标准等待(比如睡眠),但它是一个轮询循环......所以那里有一堆 1/2 秒的睡眠。该循环将在一段时间内忽略某些异常。 (就像显式等待一样,除了没有测试预期条件......)这是该驱动程序会话的全局设置。

标签: java selenium webdriverwait


【解决方案1】:

你可以这样使用:

WebElement societe = driver.findElement(By.tagName("input"));
Wait<WebElement> wait = new FluentWait<>(societe);
societe.sendKeys("Text");;
wait.until(webElement -> {
    String value = webElement.getAttribute("value");
    return "Text".equals(value);
});

你需要配置流畅的等待有一定的超时等等。

【讨论】:

    【解决方案2】:

    我会这样做:

    WebElement societe = wait.until(ExpectedConditions.elementToBeClickable(By.id("AutoComplInputBoxfld_XSociete")));
            societe.sendKeys("Text");
            while(!societe.getAttribute("value").equals("Text")) {//wait}
     societe.sendKeys(Keys.TAB);
    

    【讨论】:

      【解决方案3】:

      试试这个

      wait.until(ExpectedConditions.textToBePresentInElement(element,"Textexpected))
      

      参考

      https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#textToBePresentInElement-org.openqa.selenium.WebElement-java.lang.String-

      或者

      你可以试试

      wait.until(ExpectedConditions.textToBe(element,"Textexpected))
      

      Ps-使用设备忽略格式化

      【讨论】:

        猜你喜欢
        • 2017-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-17
        • 1970-01-01
        • 2012-03-06
        • 1970-01-01
        • 2021-05-14
        相关资源
        最近更新 更多