Selenium webdriver显式等待

时间:2013-11-19 17:18:24

标签: java selenium selenium-webdriver

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

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");

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

6 个答案:

答案 0 :(得分: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了解详情。

答案 1 :(得分:1)

public static void clickOn(WebDriver driver, WebElement locator, int timeout)
 {
        new WebDriverWait(driver,timeout).ignoring(StaleElementReferenceException.class).until(ExpectedConditions.elementToBeClickable(locator));
        locator.click();

    }

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

答案 2 :(得分:0)

我使用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

希望有所帮助。

答案 3 :(得分:0)

您的问题是您将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 :(得分:0)

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

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

//=== 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 :(得分:0)

只需使用此方法。我希望它能完美运作。

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

然后调用方法:

waitForElement("new-message-button");