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

Sri*_*han 6 java selenium webdriver wait selenium-webdriver

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");
Run Code Online (Sandbox Code Playgroud)

试图等待页面加载完成.第一个"测试"打印在控制台中,下面的例外打印在wait.until语句之外.即使在加载屏幕消失后,wait.until仍在等待.已经尝试过该元素的Staleness并且不起作用,获得相同的超时异常.加载完成后,元素在DOM中不再可用

小智 9

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

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

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


Ami*_*ith 4

您没有等待该元素在第一个语句中可见,即

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

我认为这是导致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...')]")));
Run Code Online (Sandbox Code Playgroud)

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