Wait for element in webdriver
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(baseUr开发者_开发问答l);
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?
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.
Create function as follows :
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);
}
}
and now call this function where you want to wait for element as follows:
string element="<element name>";
Wait(element);
精彩评论