Java Selenium Webdriver "Expectedconditions.not" 未按预期工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19930703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:37:41  来源:igfitidea点击:

Selenium Webdriver "Expectedconditions.not" is not working as expected

javaseleniumwebdriverselenium-webdriverwait

提问by Srivardhan

WebDriverWait wait = new WebDriverWait(driver, 60)

WebElement element = driver.findElement(By.xpath("//div[contains(text(),'Loading...')]"));
System.out.println("Test");
wait.until(ExpectedConditions.not(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[contains(text(),'Loading...')]"))));
System.out.println("Test");

Trying to wait for the page loading to be completed. The first "test" is printed in the console and below exception is printed on exceuting the wait.until statement. Even after the loading screen is gone the wait.until is still waiting. Already tried Staleness of the element also and does not work, getting the same timeout exception. Once the loading is completed the element is no more available in the DOM

尝试等待页面加载完成。第一个“测试”打印在控制台中,下面的异常在执行 wait.until 语句时打印。即使在加载屏幕消失后,wait.until 仍在等待。已经尝试过元素的陈旧并且不起作用,得到相同的超时异常。加载完成后,元素在 DOM 中不再可用

回答by Amith

you are not waiting for the element to be visible in the first statement,i.e,

您不是在等待元素在第一条语句中可见,即,

WebElement element = driver.findElement(By.xpath("//div[contains(text(),'Loading...')]"));

WebElement element = driver.findElement(By.xpath("//div[contains(text(),'Loading...')]"));

i think this is causing the NoSuchElementException...
you can try the following :

我认为这是导致NoSuchElementException...
您可以尝试以下操作:

new WebDriverWait(driver,60).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Loading...')]")));

new WebDriverWait(driver,60).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[contains(text(),'Loading...')]")));

the above code will first wait for the visibility of the element,and then its invisibility.

上面的代码将首先等待元素的可见性,然后是它的不可见性。

回答by Kirill Litvinenko

When you want to wait for element to be not present, instead of presenceOfElementLocateduse presenceOfAllElementsLocatedBy:

当你想等待元素不存在时,而不是presenceOfElementLocated使用presenceOfAllElementsLocatedBy

wait.until(ExpectedConditions.not(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//div[contains(text(),'Loading...')]"))));

It will wait until there are no elements on the page that fit the locator.

它将等到页面上没有适合定位器的元素。

回答by Ammar Ben Hadj Amor

You can try this:

你可以试试这个:

new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(d => d.FindElement(By.Id("searchTextBox0")).Displayed)

回答by Danny

If you'll be using this multiple times, create a method. E.g. if you'll be waiting for other elements elsewhere.

如果您将多次使用它,请创建一个方法。例如,如果您将在别处等待其他元素。

public void waitForElementToBeVisible(String xpath) throws Throwable {
    try {
        WebDriverWait wait = new WebDriverWait(driver, 15);
        wait.until(ExpectedConditions.or(
                ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath))
        ));
    }
    catch(Exception e) {
        System.out.println("Timeout exceeded");
        driver.close();
    }
}

You can then call this method multiple times. To call the one you're stuck with it would be

然后您可以多次调用此方法。打电话给你被困住的那个人

waitForElementToBeVisible("//div[contains(text(),'Loading...')]");

回答by hfontanez

If all you need to do is wait for the page to load, you can execute a Javascript function to check page load has completed:

如果你需要做的只是等待页面加载,你可以执行一个 Javascript 函数来检查页面加载是否完成:

String val = "";
do {
    val = (String)((JavascriptExecutor)driver).executeScript("return document.readyState");
    // DO WHATEVER
} while (!"complete".equals(val));

When using findElement()you must use implicit wait before attempting to locate the element. Otherwise, NoSuchElementExceptioncould be thrown if the component is not loaded on the page:

使用时,findElement()您必须在尝试定位元素之前使用隐式等待。否则,NoSuchElementException如果组件未加载到页面上,则可能会抛出:

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // Blocks NoSuchElementExceptions for 5 seconds when findElement() is called (set for the duration of driver's life.
WebElement element = driver.findElement(By.xpath("//div[contains(text(),'Loading...')]"));

That strategy should be used judiciously as it will most likely affect the performance of your tests. Alternatively, you should use WebDriverWait(explicit wait):

应谨慎使用该策略,因为它很可能会影响测试的性能。或者,您应该使用WebDriverWait(显式等待):

WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Loading...')]"))); // Presence of component checks the existence of the element in the DOM which it will always be true
System.out.println("Testing visibility passed...");
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[contains(text(),'Loading...')]"))); // Presence of component checks the existence of the element in the DOM which it will always be true
System.out.println("Testing invisibility passed...");

Be aware that on the last strategy, visibilityOfElementLocatedreturns a WebElementand visibilityOfElementLocatedreturns a Boolean. Therefore, you cannot chain the conditions by using .andThen(Function).

请注意,在最后一个策略中,visibilityOfElementLocated返回 aWebElementvisibilityOfElementLocated返回 a Boolean。因此,您不能使用 链接条件.andThen(Function)