【问题标题】:Selenium webdriver explicit waitSelenium webdriver 显式等待
【发布时间】:2013-12-03 09:04:54
【问题描述】:

我正在编写一些使用 selenium chrome 驱动程序的自动化测试。我试图编写一个可重用的方法,该方法将显式等待元素出现,然后在其他类中调用此方法。看起来很简单,但它没有做我想做的事。这是我的方法。

public String waitForElement(String item) {
    WebDriverWait wait = new WebDriverWait(driver,30);
    WebElement element = wait.until(
                        ExpectedConditions.elementToBeClickable(By.id(item)));
    return item;
}

然后我调用该方法并传递一个参数,如下所示:

waitForElement("new-message-button");

这似乎不起作用,有人可以提供一些见解吗?

【问题讨论】:

  • 它没有等待元素出现。
  • 你确定item是你想要返回的吗?当我编写这样的代码时,我想要的是在您的代码中名为 element 的内容。
  • 我试过也没用。
  • 你确定在 DOM 中只有该 ID 的一项吗?另外,如果您调试代码,是否找到element?
  • 你怎么知道它不等待?您是否设置了断点并对其进行了调试?代码看起来不错,我真的不相信“不等待”它“不起作用”。它会抛出超时异常吗?如果是什么时候?过了多久?为什么返回传入的相同字符串?这没有任何意义。

标签: java selenium selenium-webdriver


【解决方案1】:

您可以使用显式等待或流畅等待

显式等待示例 -

WebDriverWait wait = new WebDriverWait(WebDriverRefrence,20);
WebElement aboutMe;
aboutMe= wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("about_me")));

流畅等待示例 -

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)                            
.withTimeout(20, TimeUnit.SECONDS)          
.pollingEvery(5, TimeUnit.SECONDS)          
.ignoring(NoSuchElementException.class);    

  WebElement aboutMe= wait.until(new Function<WebDriver, WebElement>() {       
public WebElement apply(WebDriver driver) { 
return driver.findElement(By.id("about_me"));     
 }  
});

查看TUTORIAL了解更多详情。

【讨论】:

【解决方案2】:
public static void clickOn(WebDriver driver, WebElement locator, int timeout)
 {
        new WebDriverWait(driver,timeout).ignoring(StaleElementReferenceException.class).until(ExpectedConditions.elementToBeClickable(locator));
        locator.click();

    }

在 main 方法中调用上述方法,我们将得到显式等待功能。

【讨论】:

    【解决方案3】:

    您的问题是您将 String 传递给方法参数:

    public String waitForElement(String item) {

    您必须传递您的 WebElement,例如:

    public boolean visibilityOfElementWait(WebElement webElement) {
        if (webElement != null) {
            try {
                WebDriverWait wait = new WebDriverWait(Driver.getCurrentDriver(), 20);
                wait.until(ExpectedConditions.visibilityOf(wrappedElement));
                highlightElement(webElement);
                return true;
            } catch (Exception e) {
                return false;
            }
        } else
            Logger.logError("PageElement " + webElement.getText() + " not exist");
        return false;
    }
    
    public void highlightElement(WebElement element) {
        if (!Config.getProperty(Config.BROWSER).equalsIgnoreCase("ANDROIDHYBRID")) {
    
            String bg = element.getCssValue("backgroundColor");
    
            for (int i = 0; i < 4; i++) {
                Driver.getDefault()
                        .executeScript("arguments[0].style.backgroundColor = 'red'", element);
                Driver.getDefault()
                        .executeScript("arguments[0].style.backgroundColor = '" + bg + "'", element);
            }
    
       //            String highlightElementScript = "arguments[0].style.backgroundColor = 'red';";
       //            Driver.getDefault().executeScript(highlightElementScript, element);
        }
    }
    

    【讨论】:

      【解决方案4】:

      我们可以自行开发隐式等待。

      使用此代码;它也应该和隐式等待一样工作。

      //=== Start of Implicit Wait Statement ===
      
      public void implicit_Wait_ID(String str) throws Exception{
      
              for(int i=0;i<60;i++){
                  try{
                      driver.findElement(By.id(str)).isDisplayed();
                      break;
                  }catch(Exception e){Thread.sleep(2000);
      
                  }   
              }
      }
      //=== End of Implicit Wait Statement ===    
      

      通过传递 ID 值使用此方法:

      public void loginGmail() throws Exception
      {
              driver.findElement(By.id("Email")).sendKeys("Mail ID");
              driver.findElement(By.id("next")).click();
              implicit_Wait_ID("Passwd");
              driver.findElement(By.id("Passwd")).sendKeys("Pwd value");
              driver.findElement(By.id("signIn")).click();
      }
      

      如果是 Xpath、LinkText,只需为所有定位器类型创建上述方法之一,并在脚本中重复使用 n 次数。

      【讨论】:

        【解决方案5】:

        就用这个方法吧,希望能完美运行。

        public void waitForElement(String item) {
            WebDriverWait wait = new WebDriverWait(driver,30);
            WebElement element =  wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("item")));
        }
        

        然后调用方法:

        waitForElement("new-message-button");
        

        【讨论】:

          【解决方案6】:

          我用以下方式写了一个明确的等待我的硒测试:

          我声明要找到我的 WebElement,并添加了一个引用 Id 的 @FindBy 注释,如下所示:

          @FindBy(how = How.ID, using = "home")
          private WebElement home;
          

          然后我的等待一个元素加载的方法是这样写的:

          public WebElement isElementLoaded(WebElement elementToBeLoaded) {
              WebDriverWait wait = new WebDriverWait(driver, 15);
              WebElement element = wait.until(ExpectedConditions.visibilityOf(elementToBeLoaded));
              return element;
          }
          

          这使我可以按名称引用我正在等待的任何元素,这些元素已使用 find by 进行注释,而不管使用的 @FindBy 方法如何。

          【讨论】:

          • 更正,这是显式等待的示例,而不是隐式等待。很好的例子。
          【解决方案7】:

          我使用 Selenium 构建了一个包,等待是我遇到的最大问题之一。最后,你上面描述的方法是行不通的。我不得不对任何动态元素进行简单的隐式等待,如下所述

          隐式等待是告诉 WebDriver 在尝试查找一个或多个元素(如果它们不是立即可用的)时轮询 DOM 一段时间。默认设置为 0。一旦设置,就会为 WebDriver 对象实例的生命周期设置隐式等待。

          代码:

          WebDriver driver = new FirefoxDriver();
          driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
          driver.get("http://somedomain/url_that_delays_loading");
          WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));
          

          src

          希望对您有所帮助。

          【讨论】:

          • 我尝试使用它,但不清楚如何根据特定元素 ID 让 Web 驱动程序等待。例如,您可以将其写在方法中吗?
          • 正如上面所说,隐式等待会持续到驱动程序元素的生命周期。如果设置 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);在您的方法开始时,驱动程序将等待它尝试在该方法中找到的每个元素 10 秒。
          • 知道了,也试过了,没用,谢谢你的帮助。
          猜你喜欢
          • 1970-01-01
          • 2013-05-28
          • 1970-01-01
          • 1970-01-01
          • 2018-01-24
          • 2017-04-19
          • 1970-01-01
          • 2012-05-11
          相关资源
          最近更新 更多