gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:20:57 25 4
gpt4 key购买 nike

我正在尝试与 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com