Selenium 3.0 ExpectedConditions问题 [英] Selenium 3.0 ExpectedConditions issue

查看:75
本文介绍了Selenium 3.0 ExpectedConditions问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用如下的click()实现,硒将像单击按钮一样起作用,并期待下一个屏幕.但是网页不会收到点击,因此不会弹出下一个屏幕.我不太了解发生了什么,也许有些人以前曾遇到过此问题.这是点击实现:

Using a click() implementation as follows, selenium will act as if it clicked the button, and will expect the next screen. But the webpage will not receive the click so the next screen will not pop up. I don't really understand what it is going on, maybe some of you encountered this issue before. Here's the click implementation:

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

注意:在2秒钟的thread.sleep状态下,确实单击了该按钮.但是众所周知,没有人想要thread.sleep.

Note: With a thread.sleep of 2 seconds, the button is indeed clicked. But as we all know, no one wants thread.sleep.

注意2:硒2.53上不会经常发生此问题

Note2: This issue will not happen as often on selenium 2.53

注意3::我目前正在使用FireFox 49.0.1,以及geckodriver 0.11.1

Note3: I'm currently using FireFox 49.0.1, with geckodriver 0.11.1

推荐答案

我将为您提供一些有关如何实施等待的示例,也许它可以帮助您更加灵活.

I will give you some examples of how I have my wait implemented, perhaps it'll help you be more flexible.

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

I create a basic waitUntil method with a time parameter with a default time.

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

然后,我可以使用该助手方法来创建等待显示或等待可点击的状态.

Then I can use that helper method to create my wait for displayed or wait for clickable.

    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进行类似操作.

You can do the similar with 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;
}

因此,我们可以使用click方法来进行点击以使用此方法:

So we can make a click method to do our clicks to use this:

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

之所以将waitForIsDisplayed和waitForIsClickable设置为原因,是因为我可以在断言中重用它们,以使它们不那么脆弱.

The reason I make the waitForIsDisplayed and waitForIsClickable is because I can reuse those in my assertions to make them less brittle.

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

此外,您可以将wait方法与方法中指定的默认时间(5秒)一起使用,或者可以执行以下操作:

Also, you can use the wait methods with the default time specified in the method (5 seconds), or can do:

waitForIsDisplayed(locator, 20);

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

That would wait a max of 20 seconds before throwing the exception.

这篇关于Selenium 3.0 ExpectedConditions问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆