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");
试图等待页面加载完成。第一个“测试”打印在控制台中,下面的例外打印在wait.until语句之外。即使在加载屏幕消失后,wait.until仍在等待。已经尝试过该元素的Staleness并且不起作用,获得相同的超时异常。加载完成后,元素在DOM中不再可用
当你想等待元素不存在时,而不是presenceOfElementLocated
使用presenceOfAllElementsLocatedBy
:
wait.until(ExpectedConditions.not(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//div[contains(text(),'Loading...')]"))));
它会等到页面上没有适合定位器的元素。
你不是在等待第一个陈述中可见元素,即
WebElement element = driver.findElement(By.xpath(“// div [contains(text(),'Loading ...')]”));
我认为这是导致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...')]")));
上面的代码将首先等待元素的可见性,然后是它的隐形。
new WebDriverWait(driver,TimeSpan.FromSeconds(10))。until(d => d.FindElement(By.Id(“searchTextBox0”))。显示)
如果您要多次使用它,请创建一个方法。例如。如果你在其他地方等待其他元素。
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();
}
}
然后,您可以多次调用此方法。打电话给那个你坚持的人就是
waitForElementToBeVisible("//div[contains(text(),'Loading...')]");
如果您只需等待页面加载,您可以执行Javascript函数来检查页面加载是否已完成:
String val = "";
do {
val = (String)((JavascriptExecutor)driver).executeScript("return document.readyState");
// DO WHATEVER
} while (!"complete".equals(val));
使用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...')]"));
应谨慎使用该策略,因为它很可能会影响测试的性能。或者,您应该使用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...");
请注意,在最后一个策略中,visibilityOfElementLocated
返回WebElement
,visibilityOfElementLocated
返回Boolean
。因此,您不能使用.andThen(Function)
链接条件。