It's convenient to wait for an WebElement to be present with WebDriverWait and ExpectedConditions.
The problem is, what if WebElement.findElment was the only possible way to locate the element , 'cause it has no id, no name, no unique class?
WebDriverWait's constructor accepts only WebDriver as arguments, not WebElement.
I've set the implicitlyWait time, so it seems not a good idea to use try{} catch(NoSuchElementException e){}, 'cause I don't want to wait that long time for this element.
Here's the scenario:
There's one web page with a form containing many input tags. Each input tag has a format requirement.
A dynamic div tag would be present after this input tag when the format requirement is not satisfied.
As there're so many input tags, I create a general method like:
public WebElement txtBox(String name) {
return driver.findElement(By.name(name));
}
instead of creating a data member for each input tag.
Then I create a method isValid to check whether user inputs in some input are valid. All I should do in isValid is to check whether a div tag is present after inputboxToCheck, with code like this:
public boolean isValid(WebElement inputboxToCheck) {
WebElementWait wait = new WebElementWait(inputboxToCheck, 1);
try {
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("./following-sibling::div")));
return false;
} catch (TimeOutException e) {
return true;
}
}
WebElementWait is an imaginary (not exist) class which works the same way as WebDriverWait.