-1

我使用 Selenium 和 Java 编写了一个脚本。它有时工作正常,没有任何异常。但有时我会收到 TimeOutException,因为我使用了显式等待。这种行为是否与应用程序有关?可能是什么问题呢?

ChromeOptions options = new ChromeOptions();
    options.addArguments("incognito");
    WebDriver driver = new ChromeDriver(options);

    driver.get("url");

    WebDriverWait wait = new WebDriverWait(driver, 20);


    wait.until(ExpectedConditions.elementToBeClickable(By.id("usernameid")));
    driver.findElement(By.id("usernameid")).sendKeys("632145");

    wait.until(ExpectedConditions.elementToBeClickable(By.id("passwordid")));
    driver.findElement(By.id("passwordid")).sendKeys("1234");


    wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//button[@type='button']")));
    driver.findElement(By.xpath(".//button[@type='button']")).click();

脚本有时在按钮处失败。我收到超时异常。

4

2 回答 2

1

同时,只需使用带有大量等待组合的“Waiter”api..

<dependency>
   <groupId>com.imalittletester</groupId>
    <artifactId>thewaiter</artifactId>
    <version>1.0</version>
</dependency>
于 2019-05-19T07:32:43.963 回答
0

在这里,您在没有设置 ImplicitWait 的情况下对每个元素使用 WebDriverWait 20 秒。如果您确实需要在每个元素上等待 20 秒,请先将 ImplicitWait 设置为 20 秒以上,然后使用 WebDriverWait。

附带说明一下,ImplicitWait 仅适用于 findElement 和 findElements 方法。

如果我们没有设置 ImplicitWait,selenium 用来查找元素的默认超时时间是 0 秒。您可以从此 url 参考更多详细信息:selenium webdriver 超时的默认值

于 2019-05-20T09:26:57.373 回答