Selenium2 等待页面上的特定元素
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是我在 C# 中执行此操作的方法(每 250 毫秒检查一次元素是否出现):
像这样调用函数:
这有帮助吗?
This is how I do that in C# (checks every 250 milliseconds for the element to appear):
Call the function like this:
Does this help?
您可以这样做,新建一个类并添加以下方法:
不要尝试实现接口 ExpectedCondition<>,这是一个坏主意。我之前遇到了一些问题。 :)
You can do like this, new a class and add below method:
Do not try to implement interface ExpectedCondition<>, that's a bad idea. I got some problems before. :)
来自此处:
from here: