【问题标题】:Selenium 3.0 ExpectedConditions issueSelenium 3.0 ExpectedConditions 问题
【发布时间】:2016-10-18 13:53:42
【问题描述】:

使用如下的click() 实现,selenium 将表现得好像它点击了按钮,并期待下一个屏幕。但是网页不会收到点击,因此不会弹出下一个屏幕。我真的不明白这是怎么回事,也许你们中的一些人以前遇到过这个问题。这是点击实现:

public static void click(WebDriver driver, By by) {
    new WebDriverWait(driver, DEFAULT_WAIT_FOR_ELEMENT)
        .until(ExpectedConditions.elementToBeClickable(by))
        .click();
}   

注意: thread.sleep 为 2 秒,按钮确实被点击了。但众所周知,没有人想要 thread.sleep。

注意2:这个问题在 selenium 2.53 上不会经常发生

注意 3:我目前使用 FireFox 49.0.1,geckodriver 0.11.1

【问题讨论】:

    标签: java selenium junit automated-tests


    【解决方案1】:

    我会给你一些我如何实现等待的例子,也许它会帮助你更灵活。

    我创建了一个带有默认时间的时间参数的基本 waitUntil 方法。

        private void waitUntil(ExpectedCondition<WebElement> condition, Integer timeout) {
        timeout = timeout != null ? timeout : 5;
        WebDriverWait wait = new WebDriverWait(driver, timeout);
        wait.until(condition);
    }
    

    然后我可以使用该辅助方法来创建等待显示或等待可点击。

        public Boolean waitForIsDisplayed(By locator, Integer... timeout) {
        try {
            waitUntil(ExpectedConditions.visibilityOfElementLocated(locator),
                    (timeout.length > 0 ? timeout[0] : null));
        } catch (org.openqa.selenium.TimeoutException exception) {
            return false;
        }
        return true;
    }
    

    您可以对 elementToBeClickable 进行类似操作。

        public Boolean waitForIsClickable(By locator, Integer... timeout) {
        try {
            waitUntil(ExpectedConditions.elementToBeClickable(locator),
                    (timeout.length > 0 ? timeout[0] : null));
        } catch (org.openqa.selenium.TimeoutException exception) {
            return false;
        }
        return true;
    }
    

    所以我们可以创建一个点击方法来执行我们的点击来使用它:

        public void click(By locator) {
        waitForIsClickable(locator);
        driver.findElement(locator).click();               
    }
    

    我制作 waitForIsDisplayed 和 waitForIsClickable 的原因是因为我可以在我的断言中重用它们以减少它们的脆弱性。

    assertTrue("Expected something to be found, but that something did not appear",
                waitForIsDisplayed(locator));
    

    另外,您可以使用方法中指定的默认时间(5 秒)的等待方法,或者可以这样做:

    waitForIsDisplayed(locator, 20);
    

    在抛出异常之前最多等待 20 秒。

    【讨论】:

    • 问题是按钮显示出来了,而且可以点击,所以没有抛出异常。正如我看到的这个问题,当 Selenium 单击按钮时按钮没有功能,或者 Selenium 的单击很沉闷。我试图理解这一点。重新使用 Selenium 2.x,它工作得很好。
    • 如果你还没有,我会尝试调试。此外,检查您正在使用的定位器,也许使用您尝试单击的定位器的父级或子级。我不知道 selenium 3 的点击方法有任何变化
    【解决方案2】:

    可能是因为它在尝试检查可点击元素时没有找到元素。根据elementToBeClickable 的实现它假定元素存在

    我建议将 QMetry Automation Framework 用于 selenium webdriver。它提供了扩展的 webdriver 和webelement,所以当你使用这个框架时,你不需要使用这样的等待。如果你真的需要等待,它会提供等待的元素级方法,例如,

    ele.waitForVisible()
    

    【讨论】:

    • 它找到了元素,点击了元素并继续下一个动作。网页未收到点击。或者 Selenium 执行的点击是空白的,没有任何效果。
    • 在这种情况下,您应该尝试使用“nativeEvents”功能。请参阅 [DesiredCapabilities][github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities] 供参考
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    • 2021-04-25
    • 1970-01-01
    • 2020-01-26
    • 2015-06-08
    • 2011-04-20
    • 2010-10-14
    相关资源
    最近更新 更多