Selenium2 等待页面上的特定元素

发布于 2024-11-10 02:12:50 字数 776 浏览 8 评论 0原文

我正在使用 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 方法在我对页面执行任何操作之前等待下一页中的元素。

你能帮我写这个方法吗?我遇到了问题,因为我不知道如何重构上述方法以便能够传递参数。

谢谢 麦克风

I am using Selenium2(2.0-b3) web driver
I want to wait for a element to be present on the page. I can write like below and it works fine.

But I do not want to put these blocks for every page.

// 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;
            }
        });

I want to convert it into a function where I can pass elementid and the function waits for a specified time and returns me true of false based on element is found or not.

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

}

I am reading that wait does not return till page is loaded etc, but I want to write the above method so that i can click on link to a page and call waitForElementPresent method to wait for element in next page before I do anything with the page.

Can you please help me write the method, I am getting into issue because I do not know how to restructure the above method to be able to pass parameters.

Thanks
Mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

执笏见 2024-11-17 02:12:50

这就是我在 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

这有帮助吗?

This is how I do that in C# (checks every 250 milliseconds for the element to appear):

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;
}

Call the function like this:

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

Does this help?

谜兔 2024-11-17 02:12:50

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

    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<>,这是一个坏主意。我之前遇到了一些问题。 :)

You can do like this, new a class and add below method:

    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;

}

Do not try to implement interface ExpectedCondition<>, that's a bad idea. I got some problems before. :)

留蓝 2024-11-17 02:12:50

来自此处

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

from here:

WebElement myDynamicElement = (new WebDriverWait(driver, 10))
  .until(new ExpectedCondition<WebElement>(){
    @Override
    public WebElement apply(WebDriver d) {
        return d.findElement(By.id("myDynamicElement"));
    }});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文