I'm trying to create a explicit wait method in my framework to handle the user's input. The method is supposed to handle all types of search: id, xpath, and css. However, When I tried testing this method, the error is returning an odd error
Method of explicit wait
public static void isDisplaying(String variable){
wait = new WebDriverWait(driver,3);
System.out.println("Looking for: " + variable);
if (driver.findElement(By.id(variable)).isDisplayed()){
System.out.println("Found variable via id");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(variable)));
} else if (driver.findElement(By.xpath(variable)).isDisplayed()) {
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(variable)));
System.out.println("Found variable via xpath");
} else if (driver.findElement(By.cssSelector(variable)).isDisplayed()){
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(variable)));
System.out.println("Found variable via css");
} else {
System.out.println(variable +" is not displayed.");
}
}
Error that I'm receiving:
org.openqa.selenium.NoSuchElementException: Unable to locate element: #\/\/div\[\@id\=\'slideOut\-nav\'\]\/ul\/li\[2\]\/a
I have no clue why it is returning the value with all those back and forward slashes. My guess is that the application failed as soon as it tried searching the xpath string using id. But shouldn't it be moving to the next else if statement? How should I handle such issue, if this is indeed the issue?
When I simply locate the element and clicking, it works fine and returns the correct path:
//div[@id='slideOut-nav']/ul/li[2]/a
I've been stuck on this issue for a loooooong time so any help would greatly be appreciated!!
The call to driver.findElement(By.id(variable))
is bound to throw exceptions immediately if the element is not found.
Try replacing calls to driver.findElement()
with driver.findElements()
because this call does not throw exceptions but it returns an empty list of webelements when the call fails.
If you are really looking at building a generic method, then you should first parse the variable
contents to see if it represents xPath
or css
and finally fall back to using id (or) name
.
You can take a look at this implementation for reference.