【问题标题】:Selenium refresh硒刷新
【发布时间】:2013-07-08 07:12:13
【问题描述】:

我正在处理所有内容都保存在事件中的项目,因此服务器需要一些时间来响应新数据。我正在使用 Fluent 等待使用 ajax 的页面,但是这个不使用任何 ajax。所以我想刷新页面检查是否有新项目,如果没有再次刷新。这在 Selenium 2 中是如何实现的?

我这样做了:

        def accountsAfterCreating = 0
        while (accountsAfterCreating <= existingAccounts) {
            driver.navigate().refresh()
            WebElement table = wait.until(new Function<WebDriver, WebElement>() {
                public WebElement apply(WebDriver driver) {
                    return driver.findElement(By.className("table"))
                }
            })
            accountsAfterCreating = table.findElements(By.className("amount")).size()
        }

方法对吗?

【问题讨论】:

    标签: selenium groovy spock


    【解决方案1】:

    在 try catch 块中使用这样的显式等待

    试试{

    WebElement myDynamicElement = (new WebDriverWait(driver, 10))
      .until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
    

    }

    catch()
    

    {

    driver.navigate().refresh()
    

    }

    【讨论】:

      【解决方案2】:

      我通常使用这种方法来等待任何html标签。我们也可以指定等待时间。

      public boolean waitForElement(WebElement ele, String xpath, int seconds) throws InterruptedException{
          //returns true if the element appears within the time
          //false when timed out
          int t=0;
          while(t<seconds*10){
              if(ele.findElements(By.xpath(xpath)).size()>0)
                  return true;
              else{
                  Thread.sleep(100);
                  t++;
                  continue;
              }
          }       
          System.out.println("waited for "+seconds+"seconds. But couldn't find "+xpath+ " in the element specified");
          return false;
      }
      

      【讨论】:

        【解决方案3】:

        我想出了这样的答案。这仅适用于 groovy,因为它使用闭包

            private boolean refreshUntil(Closure<Boolean> condition) {
            Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
                .withTimeout(8, TimeUnit.SECONDS)
                .pollingEvery(200, TimeUnit.MILLISECONDS)
                .ignoring(NoSuchElementException)
            wait.until(new Predicate<WebDriver>() {
                boolean apply(WebDriver driver) {
                    driver.navigate().refresh()
                    if (condition()) {
                        return true
                    }
                    return false
                }
            })
            return true
        }
        

        并调用此方法

        refreshUntil {
                    accountsBeforeCreation + 1 == driver.findElements(By.tagName("tr"))).size()
                }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-03-03
          • 1970-01-01
          • 2021-04-15
          • 2018-08-16
          • 2012-10-01
          • 2018-02-06
          • 2012-05-13
          • 1970-01-01
          相关资源
          最近更新 更多