【问题标题】:Selenium Webdriver "Expectedconditions.not" is not working as expectedSelenium Webdriver“Expectedconditions.not”未按预期工作
【发布时间】:2013-11-24 16:36:33
【问题描述】:
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 中不再可用

【问题讨论】:

    标签: java selenium webdriver selenium-webdriver wait


    【解决方案1】:

    你可以试试这个:

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

    【讨论】:

      【解决方案2】:

      如果您只需要等待页面加载,您可以执行 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) 链接条件。

      【讨论】:

        【解决方案3】:

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

        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...')]");
        

        【讨论】:

          【解决方案4】:

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

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

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

          【讨论】:

            【解决方案5】:

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

            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...')]")));
            

            上面的代码会先等待元素的可见性,然后再等待它的不可见性。

            【讨论】:

            • 我已经尝试过了,第一个语句工作正常,但第二个语句在加载元素消失后继续等待整整 60 秒,所以我最终等待整整 60 秒。跨度>
            猜你喜欢
            • 1970-01-01
            • 2017-12-13
            • 1970-01-01
            • 2013-12-10
            • 1970-01-01
            • 2022-01-23
            • 1970-01-01
            • 2016-11-29
            • 1970-01-01
            相关资源
            最近更新 更多