【问题标题】:How to wait until an element is present in Selenium?如何等到硒中存在元素?
【发布时间】:2014-01-21 02:21:26
【问题描述】:

我试图让 Selenium 等待页面加载后动态添加到 DOM 的元素。试过这个:

fluentWait.until(ExpectedConditions.presenceOfElement(By.id("elementId"));

如果有帮助,这里是fluentWait

FluentWait fluentWait = new FluentWait<>(webDriver) {
    .withTimeout(30, TimeUnit.SECONDS)
    .pollingEvery(200, TimeUnit.MILLISECONDS);
}

但它会抛出一个NoSuchElementException - 看起来presenceOfElement 期望元素在那里,所以这是有缺陷的。这必须是 Selenium 的面包和黄油,并且不想重新发明轮子......有人可以提出替代方案,最好不要自己滚动Predicate

【问题讨论】:

    标签: java selenium selenium-webdriver wait predicate


    【解决方案1】:

    您需要调用 ignoring 以忽略异常,而 WebDriver 将等待。

    FluentWait<WebDriver> fluentWait = new FluentWait<>(driver)
            .withTimeout(30, TimeUnit.SECONDS)
            .pollingEvery(200, TimeUnit.MILLISECONDS)
            .ignoring(NoSuchElementException.class);
    

    有关更多信息,请参阅FluentWait 的文档。但请注意,此条件已在 ExpectedConditions 中实现,因此您应该使用

    WebElement element = (new WebDriverWait(driver, 10))
       .until(ExpectedConditions.elementToBeClickable(By.id("someid")));
    

    *Update for newer versions of Selenium:

    withTimeout(long, TimeUnit) has become withTimeout(Duration)
    pollingEvery(long, TimeUnit) has become pollingEvery(Duration)
    

    所以代码看起来是这样的:

    FluentWait<WebDriver> fluentWait = new FluentWait<>(driver)
            .withTimeout(Duration.ofSeconds(30)
            .pollingEvery(Duration.ofMillis(200)
            .ignoring(NoSuchElementException.class);
    

    等待的基础教程可以在here找到。

    【讨论】:

    • 它应该是 或者它应该是 如果你使用的是 java 8 会给你编译错误。
    • 它没有在 ExpectedCondition 中实现....它在 WebDriverWait 中。 public WebDriverWait(WebDriver driver, Clock clock, Sleeper sleeper, long timeOutInSeconds, long sleepTimeOut) { super(driver, clock, sleeper); this.withTimeout(timeOutInSeconds, TimeUnit.SECONDS); this.pollingEvery(sleepTimeOut, TimeUnit.MILLISECONDS); this.ignoring(NotFoundException.class); this.driver = 驱动程序; }
    【解决方案2】:
    WebDriverWait wait = new WebDriverWait(driver,5)
    wait.until(ExpectedConditions.visibilityOf(element));
    

    您可以在加载整个页面代码执行并抛出错误之前使用此时间。时间是秒

    【讨论】:

      【解决方案3】:

      让我推荐你使用Selenide 库。 它允许编写更简洁和可读的测试。它可以等待语法更短的元素出现:

      $("#elementId").shouldBe(visible);
      

      这是一个测试 Google 搜索的示例项目: https://github.com/selenide-examples/google

      【讨论】:

      • 不可能将其移植到 python 中?
      • 它已移植到 Python。请参阅“Selene”python 库。
      【解决方案4】:
      public WebElement fluientWaitforElement(WebElement element, int timoutSec, int pollingSec) {
      
          FluentWait<WebDriver> fWait = new FluentWait<WebDriver>(driver).withTimeout(timoutSec, TimeUnit.SECONDS)
              .pollingEvery(pollingSec, TimeUnit.SECONDS)
              .ignoring(NoSuchElementException.class, TimeoutException.class).ignoring(StaleElementReferenceException.class);
      
          for (int i = 0; i < 2; i++) {
              try {
                  //fWait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//*[@id='reportmanager-wrapper']/div[1]/div[2]/ul/li/span[3]/i[@data-original--title='We are processing through trillions of data events, this insight may take more than 15 minutes to complete.']")));
              fWait.until(ExpectedConditions.visibilityOf(element));
              fWait.until(ExpectedConditions.elementToBeClickable(element));
              } catch (Exception e) {
      
              System.out.println("Element Not found trying again - " + element.toString().substring(70));
              e.printStackTrace();
      
              }
          }
      
          return element;
      
          }
      

      【讨论】:

        【解决方案5】:

        FluentWait 抛出 NoSuchElementException 是混淆的情况

        org.openqa.selenium.NoSuchElementException;     
        

        java.util.NoSuchElementException
        

        .ignoring(NoSuchElementException.class)
        

        【讨论】:

          猜你喜欢
          • 2017-10-28
          • 1970-01-01
          • 2011-12-08
          • 2018-10-23
          • 2019-12-14
          • 2016-05-11
          • 1970-01-01
          相关资源
          最近更新 更多