繁体   English   中英

Selenium Webdriver“ Expectedconditions.not”无法按预期工作

[英]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");

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

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

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

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

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

WebElement元素= 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")).Displayed)

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

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返回WebElementvisibilityOfElementLocated返回一个Boolean 因此,不能使用.andThen(Function)链接条件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM