简体   繁体   English

Selenium 3.0 ExpectedConditions问题

[英]Selenium 3.0 ExpectedConditions issue

Using a click() implementation as follows, selenium will act as if it clicked the button, and will expect the next screen. 使用如下的click()实现,硒将像单击按钮一样起作用,并期待下一个屏幕。 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();
}   

Note: With a thread.sleep of 2 seconds, the button is indeed clicked. 注意:在2秒钟的thread.sleep中,确实单击了该按钮。 But as we all know, no one wants thread.sleep. 但是众所周知,没有人想要thread.sleep。

Note2: This issue will not happen as often on selenium 2.53 注意2:在硒2.53上不会经常发生此问题

Note3: I'm currently using FireFox 49.0.1, with geckodriver 0.11.1 注意3:我目前正在使用FireFox 49.0.1,以及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. 我将为您提供一些有关如何实施等待的示例,也许它将帮助您更加灵活。

I create a basic waitUntil method with a time parameter with a default time. 我使用带有默认时间的时间参数创建了一个基本的waitUntil方法。

    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;
}

You can do the similar with elementToBeClickable. 您可以使用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;
}

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();               
}

The reason I make the waitForIsDisplayed and waitForIsClickable is because I can reuse those in my assertions to make them less brittle. 之所以选择waitForIsDisplayed和waitForIsClickable,是因为我可以在断言中重用它们,以使它们不那么脆弱。

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

Also, you can use the wait methods with the default time specified in the method (5 seconds), or can do: 另外,您可以将wait方法与方法中指定的默认时间(5秒)一起使用,或者可以执行以下操作:

waitForIsDisplayed(locator, 20);

That would wait a max of 20 seconds before throwing the exception. 在抛出异常之前,最多等待20秒。

May be the reason is it didn't find element while it trying to check element clickable. 可能是因为它在尝试检查元素可点击时找不到元素。 As per implementation of elementToBeClickable it assumes element present . 根据elementToBeClickable实现, 它假定element存在

I would suggest to use QMetry Automation Framework for selenium webdriver. 我建议对硒webdriver使用QMetry自动化框架 It provides extended webdriver and webelement so when you are using this framework you don't need to use such waits. 它提供了扩展的webdriver和webelement,因此当您使用此框架时,无需使用此类等待。 In case you really required wait it provides element level methods for wait, for instance, 如果您确实需要等待,它会提供元素级别的等待方法,例如,

ele.waitForVisible()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Selenium 潜在铬 Cookies,驱动程序,预期条件 ZDB974238714CA38A434A7CE1D8 - Java Selenium Potential Chrome Cookies, Driver, ExpectedConditions API Issue? Selenium ExpectedConditions NOT elementToBeClickable - Selenium ExpectedConditions NOT elementToBeClickable 如何在 Selenium 4 中使用 ExpectedConditions? - How to use ExpectedConditions in Selenium 4? Selenium更新后,ExpectedConditions将不起作用 - ExpectedConditions won't work after Selenium update Selenium Webdriver“ Expectedconditions.not”无法按预期工作 - Selenium Webdriver “Expectedconditions.not” is not working as expected Selenium 等待 ExpectedConditions.attributeToBe 的行为不符合预期 - Selenium wait for ExpectedConditions.attributeToBe is not behaving as expected 硒:我应该选择什么期望条件 - Selenium: What ExpectedConditions should i select 在运行硒测试时,ExpectedConditions类会引发许多错误 - ExpectedConditions class throws numerous errors when running selenium test 为什么我的ExpectedConditions命令被忽略了? Java中的Selenium WebDriver - Why are my ExpectedConditions commands being ignored? Selenium WebDriver in Java WebDriverWait wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(by))与Selenium 3.4.0不兼容 - WebDriverWait wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(by)) not working with Selenium 3.4.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM