【问题标题】:Selenium2 wait for specific element on a pageSelenium 等待页面上的特定元素
【发布时间】:2011-05-28 06:33:47
【问题描述】:

我正在使用 Selenium2(2.0-b3) 网络驱动程序 我想等待页面上出现一个元素。我可以像下面这样写,而且效果很好。

但我不想为每个页面都放置这些块。

// Wait for search to complete
        wait.until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver webDriver) {
                System.out.println("Searching ...");
                return webDriver.findElement(By.id("resultStats")) != null;
            }
        });

我想将它转换成一个函数,我可以在其中传递 elementid,该函数等待指定的时间并根据是否找到元素返回 true 或 false。

public static boolean waitForElementPresent(WebDriver driver, String elementId, int noOfSecToWait){

}

我正在阅读等待在页面加载之前不会返回等,但我想编写上述方法,以便我可以单击页面链接并调用 waitForElementPresent 方法以等待下一页中的元素,然后再执行任何操作与页面。

你能帮我写一下方法吗,我遇到了问题,因为我不知道如何重组上述方法才能传递参数。

谢谢 迈克

【问题讨论】:

    标签: wait webdriver selenium-webdriver


    【解决方案1】:

    这就是我在 C# 中的做法(每 250 毫秒检查一次元素是否出现):

    private bool WaitForElementPresent(By by, int waitInSeconds)
    {
    var wait = waitInSeconds * 1000;
        var y  = (wait/250);
        var sw = new Stopwatch();
        sw.Start();
    
        for (var x = 0; x < y; x++)
        {
            if (sw.ElapsedMilliseconds > wait) 
                return false;
    
            var elements = driver.FindElements(by);
            if (elements != null && elements.count > 0)
                return true;
            Thread.Sleep(250);
        }
        return false;
    }
    

    这样调用函数:

    bool found = WaitForElementPresent(By.Id("resultStats"), 5);  //Waits 5 seconds
    

    这有帮助吗?

    【讨论】:

      【解决方案2】:

      你可以这样做,新建一个类并添加以下方法:

          public WebElement wait4IdPresent(WebDriver driver,final String elementId, int timeOutInSeconds){
      
          WebElement we=null;
          try{
              WebDriverWait wdw=new WebDriverWait(driver, timeOutInSeconds);
      
              if((we=wdw.until(new ExpectedCondition<WebElement>(){
                  /* (non-Javadoc)
                   * @see com.google.common.base.Function#apply(java.lang.Object)
                   */
                  @Override
                  public WebElement apply(WebDriver d) {
                      // TODO Auto-generated method stub
                      return d.findElement(By.id(elementId));
                  }
              }))!=null){
                  //Do something;
              }
          }catch(Exception e){
              //Do something;
          }
          return we;
      
      }
      

      不要尝试实现接口ExpectedCondition,这是个坏主意。我之前遇到了一些问题。 :)

      【讨论】:

        【解决方案3】:

        来自here

        WebElement myDynamicElement = (new WebDriverWait(driver, 10))
          .until(new ExpectedCondition<WebElement>(){
            @Override
            public WebElement apply(WebDriver d) {
                return d.findElement(By.id("myDynamicElement"));
            }});
        

        【讨论】:

          猜你喜欢
          • 2021-11-14
          • 2013-01-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-07
          • 2019-04-10
          • 2021-02-17
          • 2017-02-21
          相关资源
          最近更新 更多