Reputation: 65
I had gone through many google answers for how to make sure element availability before we do action on any web element to avoid NoSuchElementException exception.
WebDriver driver = new FirefoxDriver();
driver.findElement(By.id("userid")).sendKeys("XUser");
Here line #2 will throw NoSuchElementException if the element is not available on page.
I just want to avoid this Exception to be thrown.
There are many methods available to check this in WebDriver.
isDisplayed()
isEnabled()
driver.findElements(By.id("userid")).size() != 0
driver.findElement(By.id("userid")).size() != null
driver.getPageSource().contains("userid")
Which is best one in the above methods to make sure element availability? Why?
Is there any other methods available apart from these?
Thanks in Advance. Thank you for your valuable time.
Upvotes: 3
Views: 2303
Reputation: 910
You can write a generic method which can check the existence of the required Webelement before performing any operation on it. For example, following method is capable of checking the existence of a Webelement based on all the supported criteria e.g. xpath, id, name, tagname, class etc.
public static boolean isElementExists(By by){
return wd.findElements(by).size() !=0;
}
For instance, if you need to find the existence of a Webelement based on its xpath, you can use above method in following way:
boolean isPresent = isElementExists(By.xpath(<xpath_of_webelement>);
if(isPresent){
//perform the required operation
} else {
//Avoid operation and perform necessary actions
}
Upvotes: 0
Reputation: 36107
You can use any of methods listed in your questions - there is no the best or the worst method.
There are aslo some other methods - two proposed by @Eby and @Umang in their answers, an also the below method which does not wait for the element, just cheks if the element is present or not at this moment:
if( driver.findElements(By.id("userid")).count > 0 ){
System.out.println("This element is available on the page");
}
else{
System.out.println("This element is not available on the page");
}
However a requirement is::
line #2 will throw ""NoSuchElementException" if the element does not available on page.
I just want to avoid this Exception to be thrown.
then in my opinion the easiest method is:
try{
driver.findElement(By.id("userid")).sendKeys("XUser");
}catch( NoSuchElementException e ){
System.out.println("This element is not available on the page");
-- do some other actions
}
Upvotes: 1
Reputation: 26
Try to use explicit wait of selenium API.
Wait for some time until your required element available on webpage. You can try following example:
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("userid"))));
So above line will wait for element till 10 seconds, if the element available in less than 10 seconds then it will stop waiting and move ahead for execution.
Upvotes: 1
Reputation: 396
public boolean isElementPresentById(String targetId) {
boolean flag = true;
try {
webDrv.findElement(By.id(targetId));
} catch(Exception e) {
flag = false;
}
return flag;
}
Upvotes: 2