Selenium webdriver显式等待 [英] Selenium webdriver explicit wait

查看:175
本文介绍了Selenium webdriver显式等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing some automated tests for using the selenium chrome driver. I trying to write a reusable method that will explicitly wait for elements to appear and then call this method in other classes. Seems pretty straight forward but its not doing what I want it do. Here is the method that I have.

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

然后我调用方法并传递一个这样的参数:

Then I call the method and pass it a parameter like this:

waitForElement("new-message-button");

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

That doesn't seem to become working, can someone give some insight?

推荐答案

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

You can use Explicit wait or Fluent Wait

显式等待示例 -

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 了解更多详情。

这篇关于Selenium webdriver显式等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆