WebDriver Wait '.until(ExpectedConditions.elementToBeClickable' 仅当我将 Thread.sleep(500) 添加到代码中时才有效?

2024-02-06

  1. 我正在尝试使用 webdriver wait 单击页面上可见的按钮,但 webdriver 仅在添加后才能单击该按钮Thread.sleep到代码。

  2. 在执行代码之前,我还检查了按钮是否可见(True)returns = true.

//按钮可见性检查:

List<WebElement> signOutList = driver.findElements(.xpath(".//*[starts-with(@id,'fulfillment-time-type-section-')]/div[2]//button"));
Assert.assertTrue(signOutList.size() > 0);

//下面的代码没有点击按钮

By timeDropdownButton = By.xpath(".//*[starts-with(@id,'fulfillment-time-type-section-')]/div[2]//button");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
              .until(ExpectedConditions.elementToBeClickable(timeDropdownButton));
myDynamicElement.click();

//下面的代码是否点击了按钮:

Thread.sleep(500);
By timeDropdownButton = By.xpath(".//*[starts-with(@id,'fulfillment-time-type-section-')]/div[2]//button");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
              .until(ExpectedConditions.elementToBeClickable(timeDropdownButton));
myDynamicElement.click();


请注意,我还尝试使用 JS 代码和 WebDriver 操作等单击按钮

我不知道为什么“Wait Clickable”仅在与“Thread.sleep”结合使用时才起作用?

我试图点击的按钮 https://i.stack.imgur.com/yIBxn.png


你想避免Thread.sleep一般来说,在测试中尽可能多。仅通过一些测试似乎并不那么重要,但它们的使用中存在很多固有的问题。首先,如果您有大量测试,测试套件的运行时间可能会变得难以管理。其次,它们有时还不够。例如等待500毫秒might一般来说,对于生产机器来说已经足够了,但是如果 Web 服务器负载很重或在测试环境中,则可能需要 750 毫秒才能准备好。那么你正在处理非确定性故障。最好使用类似的结构WebDriverWait并给它们一个合理的(但过于慷慨的)最大值,这样您就不必等待超过必要的时间,但如果失败,则意味着测试环境存在严重问题。

此外,这里的必胜客网站大量使用了异步 java 脚本和浮动面板等。所有这些在使用 Selenium 时都需要多加小心,因为元素在与元素交互之前需要做好准备,但它们通常已经在页面中了。 DOM。这意味着 Selenium 默认情况下可能会在 JavaScript 完成之前快速找到它们,并且它们实际上并未处于准备使用状态。您将想要使用 ExpectedConditions,就像您已经尝试做的那样。您遇到问题的按钮需要等待 JavaScript 完成,这已经建议,但它不需要在页面加载时,而是在单击按钮之前。在我的机器上运行的示例:

@Test
public void foo() {
    WebDriver driver = new FirefoxDriver();
    driver.manage()
          .window()
          .maximize();
    // Move through the page to the point you are interested in
    driver.get("https://www.pizzahut.co.uk/");
    waitForElement(driver, By.cssSelector(".hidden-xs [title='Pizza']")).click();
    waitForElement(driver, By.cssSelector("form[action='/menu/startyourorder']")).submit();
    WebElement postElement = waitForElement(driver, By.id("ajax-postcode-txt"));
    postElement.sendKeys("TS1 4AG" + Keys.ENTER);
    // Wait for the java script to finish executing
    waitForJavaScript(driver);
    // Finally you should be able to click on the link
    waitForElement(driver, By.partialLinkText("Start Your Order")).click();
    // continue on ... then quit the driver
    driver.quit();
}

private WebElement waitForElement(WebDriver driver, By locator) {
    return new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(locator));
}

private void waitForJavaScript(WebDriver driver) {
    new WebDriverWait(driver, 10).until(new Predicate<WebDriver>() {
                                            public boolean apply(WebDriver driver) {
                                                return ((JavascriptExecutor) driver).executeScript("return document.readyState")
                                                                                    .equals("complete");
                                            }
                                        }
    );
}

在 WebDriverWait 中,给定的 int 参数是它将继续尝试给定检查的秒数。如果达到设定数量(10上例中的秒)它会抛出一个TimeoutException.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WebDriver Wait '.until(ExpectedConditions.elementToBeClickable' 仅当我将 Thread.sleep(500) 添加到代码中时才有效? 的相关文章

随机推荐