Reputation: 1943
I need to find if an element is displayed or not. How to check that in selenium web driver?
if(driver.findElement(By.id("p_first_name")).isDisplayed())
{
WebElement fname =driver.findElement(By.id("p_first_name"));
fname.sendKeys("pradnya");
WebElement lname = driver.findElement(By.xpath("//*[@id=\"p_last_name\"]"));
lname.sendKeys("Bolli");
WebElement Address1 = driver.findElement(By.xpath("//*[@id=\"address_11\"]"));
Address1.sendKeys("New address1");
WebElement Address2 = driver.findElement(By.xpath("//*[@id=\"address_21\"]"));
Address2.sendKeys("New address2");
WebElement City = driver.findElement(By.xpath("//*[@id=\"city1\"]"));
City.sendKeys("Pune");
WebElement Country = driver.findElement(By.xpath("//*[@id=\"country1\"]"));
Country.sendKeys("India");
WebElement ZipCode = driver.findElement(By.xpath("//*[@id=\"pincode1\"]"));
ZipCode.sendKeys("India");
WebElement State = driver.findElement(By.xpath("//*[@id=\"bds\"]"));
State.sendKeys("Maharashtra");
}
else
{
WebElement address = driver.findElement(By.xpath("//*[@id=\"update_add77\"]"));
address.click();
}
On the checkout page first it shows address form and when user fils that then listing is shown. Address form is not showing when the listing is showing. In this case, how to check if an address form field is displayed or not?
I use above code but it gives me exception message
'Unable to locate element: #p_first_name'
Upvotes: 0
Views: 2145
Reputation: 177
public boolean isElementPresent(By element,int timeOutInSeconds,int pollingEveryInMiliSec) {
try {
WebDriverWait wait = new WebDriverWait(d, timeOutInSeconds);
wait.pollingEvery(pollingEveryInMiliSec, TimeUnit.MILLISECONDS);
wait.ignoring(NoSuchElementException.class);
wait.ignoring(ElementNotVisibleException.class);
wait.ignoring(StaleElementReferenceException.class);
wait.ignoring(NoSuchFrameException.class);
wait.until(ExpectedConditions.visibilityOfElementLocated(element) ));
return true;
}
catch(Exception e) {
return false;
}
}
If you consider timeOutInSeconds=20 and pollingEveryInMiliSec=5 in every 5 ms this method will search for the giving element until it find's it with in 20ms
Upvotes: 0
Reputation: 2814
You can create method for such check. We are using NoSuchElementException
to verify element is not existing.
public boolean isElementExist(By locator)
{
try {
driver.findElement(locator);
} catch (NoSuchElementException e) {
return false;
}
return true;
}
Or due slow loading and timeouts, i advice to use *WebDriverWait*
Upvotes: 0
Reputation: 4507
The element is giving NoSuchElementException
as the element is not present on the UI on which you are trying to find it using the isDisplayed()
method.
So, to solve your problem you should fetch the list of the element and then can get the size of that list, if the size is greater than 0, it means the element is present on the page else the element is not present.
You need to make the following changes in the code:
if(driver.findElements(By.id("p_first_name")).size()>0){
// Add the if code here
}
else{
// Add the else code here
}
Upvotes: 3