等待 webdriver 中的元素
我按照这里写的: WebDriver Selenium API:当 Element 明显存在时出现 ElementNotFoundErrorException!
我的代码看起来像:
Function<WebDriver, WebElement> presenceOfElementLocated(final By locator) {
return new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(locator);
}
};
}
.......
driver.get(baseUrl);
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(presenceOfElementLocated(By.className("classname")));
findByClassAndName(driver, "x-tree3-node-text", "Name1").click();
问题是,那似乎没有做任何事情。它不起作用,我什至看不到一丝等待网页 GUI 的痕迹。我通过超时隐式等待也得到了同样的结果...任何人都可以帮忙吗?
I followed what was written here:
WebDriver Selenium API: ElementNotFoundErrorException when Element is clearly there !
My code looks like:
Function<WebDriver, WebElement> presenceOfElementLocated(final By locator) {
return new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(locator);
}
};
}
.......
driver.get(baseUrl);
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(presenceOfElementLocated(By.className("classname")));
findByClassAndName(driver, "x-tree3-node-text", "Name1").click();
Problem is, that is does not seem to do anything. It doesn't work and i can't even see slightest trace of waiting for webpage gui. i got the same with implicit wait through timeouts... Anyone could help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(2)
一身软味2024-12-15 16:32:33
创建函数如下:
public void Wait (string element) // Wait function to wait for element
{
for (int second = 0; ; second++)
{
if (second >= 60) Assert.Fail("timeout");
try
{
if (IsElementPresent(By.LinkText(element))) break;
}
catch (Exception)
{ }
Thread.Sleep(1000);
}
}
现在在您想要等待元素的地方调用此函数,如下所示:
string element="<element name>";
Wait(element);
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您必须捕获从 ExpectedCondition 或 Function 抛出的 Throwables(apply() 方法是一个很好的地方)并返回 null,以便 Wait.until() 继续运行 -请参阅http://rostislav-matl.blogspot.com/2011/05/moving-to-selenium-2-on-webdriver-part.html 了解详细示例。
You have to catch Throwables throwed from ExpectedCondition or your Function (the apply() methods is good place for that) and return null so Wait.until() continues to run - see http://rostislav-matl.blogspot.com/2011/05/moving-to-selenium-2-on-webdriver-part.html for detailed example.