Selenium2等待页面上的特定元素

| 我正在使用Selenium2(2.0-b3)Web驱动程序 我想等待页面上出现一个元素。我可以像下面这样写,并且工作正常。 但是我不想在每个页面上都放这些块。
// 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的函数,并且该函数等待指定的时间,然后根据是否找到element返回我的false的true。 公共静态布尔值waitForElementPresent(WebDriver驱动程序,字符串elementId,int noOfSecToWait){ } 我正在阅读等到页面加载后才会返回的等待,但是我想编写上述方法,以便我可以单击页面链接并调用waitForElementPresent方法以等待下一页中的元素,然后再对该页面执行任何操作。 您能帮我编写该方法吗,我遇到了问题,因为我不知道如何重组上述方法以便能够传递参数。 谢谢 麦克风     
已邀请:
        这就是我在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
这有帮助吗?     
        您可以这样做,新建一个类并添加以下方法:
    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 <>,这不是一个好主意。我以前有一些问题。 :)     
        从这里:
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
  .until(new ExpectedCondition<WebElement>(){
    @Override
    public WebElement apply(WebDriver d) {
        return d.findElement(By.id(\"myDynamicElement\"));
    }});
    

要回复问题请先登录注册