is there a way how to test if an element is present? Any findElement method would end in an exception, but that is not what I want, because it can be that an element is not present and that is okay, that is not a fail of the test, so an exception can not be the solution.
I've found this post: Selenium c# Webdriver: Wait Until Element is Present But this is for C# and I am not very good at it. Can anyone translate the code into Java? I am sorry guys, I tried it out in Eclipse but I don't get it right into Java code.
This is the code:
public static class WebDriverExtensions{
public static IWebElement FindElement(this IWebDriver driver, By by, int timeoutInSeconds){
if (timeoutInSeconds > 0){
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(drv => drv.FindElement(by));
}
return driver.FindElement(by);
}
}
Simplest way I found in Java is:
This should do it:
To find a particular Element is present or not, we have to use findElements() method instead of findElement()..
this is worked for me.. suggest me if i am wrong..
You can try implicit wait:
`
Or You can try explicit wait one: `
`
Explicit will check if element is present before some action. Implicit wait could be call in every place in the code. For example after some AJAX actions.
More you can find at SeleniumHQ page
I would use something like (with Scala [the code in old "good" Java 8 may be similar to this]):
so then,