[英]Selenium Webdriver “Expectedconditions.not” is not working as expected
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.
第一个“测试”打印在控制台中,下面的异常打印在执行wait.until语句时。 Even after the loading screen is gone the wait.until is still waiting.
即使在加载屏幕消失后,wait.until仍在等待。 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
加载完成后,该元素在DOM中不再可用
When you want to wait for element to be not present, instead of presenceOfElementLocated
use 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. 它将等到页面上没有适合定位器的元素。
you are not waiting for the element to be visible in the first statement,ie, 您不是在等待该元素在第一条语句中可见,即
WebElement element = driver.findElement(By.xpath("//div[contains(text(),'Loading...')]"));
WebElement元素= driver.findElement(By.xpath(“ // div [contains(text(),'Loading ...')]”)));
i think this is causing the NoSuchElementException
... 我认为这导致
NoSuchElementException
...
you can try the following : 您可以尝试以下方法:
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. 上面的代码将首先等待元素的可见性,然后等待其不可见性。
您可以尝试以下方法:
new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(d => d.FindElement(By.Id("searchTextBox0")).Displayed)
If you'll be using this multiple times, create a method. 如果要多次使用,请创建一个方法。 Eg 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...')]");
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. 使用
findElement()
,必须在尝试定位元素之前使用隐式等待。 Otherwise, NoSuchElementException
could be thrown if the component is not loaded on the page: 否则,如果未在页面上加载组件,则可能引发
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, visibilityOfElementLocated
returns a WebElement
and visibilityOfElementLocated
returns a Boolean
. 要知道,在过去的策略,
visibilityOfElementLocated
返回WebElement
和visibilityOfElementLocated
返回一个Boolean
。 Therefore, you cannot chain the conditions by using .andThen(Function)
. 因此,不能使用
.andThen(Function)
链接条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.