java - 与implicitwait()同步不起作用,为什么?

标签 java selenium selenium-webdriver

我正在尝试与 selenium webdriver 同步,但隐式等待()有些东西不起作用。

我对implicitlyWait(..)的理解是,代码等待,直到元素在最大时间内可用。

下面的代码因错误而崩溃:

org.openqa.selenium.InvalidElementStateException: invalid element state: Element is not currently interactable and may not be manipulated

System.out ist 打印: -->> false true false (isDiplayed(), isEnabled(), is Selected())

private static WebDriver driver;    
public static void main(String[] args) throws InterruptedException {
         setupWebDriverChrome();
        //Thread.sleep(1000);
        final String cssSelectorFromAirport = "div.od-airportselector.airportselector_root input[tabindex='11']";
        final By cssSelector = By.cssSelector(cssSelectorFromAirport);
        WebElement fromAirportElement = driver.findElement(cssSelector);
        System.out.println("-->> " + fromAirportElement.isDisplayed() + " " + fromAirportElement.isEnabled() + " " + fromAirportElement.isSelected());       
        fromAirportElement.clear();
        fromAirportElement.sendKeys("MUC");
    }

    private static void setupWebDriverChrome() {
        System.setProperty("webdriver.chrome.driver", "C:\\...\\chromedriver.exe");
        setupLocation();
    }
    private static void setupLocation() {
        driver.manage().timeouts().implicitlyWait(1000, TimeUnit.MILLISECONDS);     
        driver.get("https://www.opodo.de/");
    }

我也用 Geckodriver 尝试过,得到了相同的结果。

我也增加了等待时间,但结果相同。

使其工作的唯一方法是使用 Thread.sleep() (上面已注释)

编辑 请。请注意,我没有看到 Selenium implicitwait not working 有任何重复。 .

最佳答案

您必须等待您的元素可点击。尝试添加以下内容:

 WebElement element = (new WebDriverWait(driver, 10))
                .until(ExpectedConditions.elementToBeClickable(By.cssSelector(cssSelectorFromAirport)));

所以:

    setupWebDriverChrome();
    //Thread.sleep(1000);
    final String cssSelectorFromAirport = "div.od-airportselector.airportselector_root input[tabindex='11']";
    WebElement element = (new WebDriverWait(driver, 10))
            .until(ExpectedConditions.elementToBeClickable(By.cssSelector(cssSelectorFromAirport)));
    /*final By cssSelector = By.cssSelector(cssSelectorFromAirport);
    WebElement fromAirportElement = driver.findElement(cssSelector);
    System.out.println("-->> " + fromAirportElement.isDisplayed() + " " + fromAirportElement.isEnabled() + " " + fromAirportElement.isSelected());*/
    element.clear();
    element.sendKeys("MUC");

编辑

来自documentation :

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.

这意味着,在您的示例中,selenium 找到了该元素,但它尚未“可点击”。

您也可以在测试中看到这一点。如果您看一下:

System.out.println("-->> " + fromAirportElement.isDisplayed() + " " + fromAirportElement.isEnabled() + " " + fromAirportElement.isSelected() );

失败时输出为:

-->> false true false

当它工作时:

-->> true true false

关于java - 与implicitwait()同步不起作用,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46844186/

相关文章:

Java - 计算两个文档中的单词

java - 使用列表反序列化 JSON 包装对象会返回 null 属性

python - 使用 xpath 按值查找没有 value 属性的输入字段

java - 安全连接失败

java - Selenium 3.3.1 和 FirefoxDriver 的依赖性错误

java - 如何为可见的弹出窗口应用条件,

使用 Selenium Web 驱动程序的 Flash 自动化

java - 如何在没有终端的情况下在其他linux计算机上打开java程序?

java - 我想继续要求输入,直到表单扫描仪提供有效的输入

java - 通知(警报)在 selenium webdriver 的整个应用程序中随机出现