1

I want to check if an element is visible/present/detected by Selenium Web Driver for the following XPath:

//*[@data-animate-modal-popup="true"]

Is there any Selenium's function that returns TRUE or FALSE whenever that element is visible/present/detected?

Last time I was using the following IF - Else

phone_number_invalid = driver.find_element_by_xpath('//*[@data-animate-modal-popup="true"]')
if phone_number_invalid:
     code here .....

That find_element_by_xpath always throws an error whenever it doesn't find the element. I want to only get TRUE or FALSE whenever the element is visible/present/detected.

Thank you.

3 Answers3

0

There is no native element to check if the element is present you can use :

create a function:

public boolean isElementPresent(By by){
        try{
            driver.findElement(by);
            return true;
        }
        catch(NoSuchElementException e){
            return false;
        }
    }

and now call it in your if case:

 if(isElementPresent(By.xpath("//div[contains(text(),'Report name already exists. Please enter another name.')]")))
   {
       //code
   }
   else if(isElementPresent(By.xpath("//div//span[contains(text(),'Grid Report saved successfully.')]")))
   {
       //code
   }

Second option:

List<WebElement> element1 = driver.findElement(By.xpath("//div[contains(text(),'Report name already exists. Please enter another name.')]"));
   List<WebElement> element2 = driver.findElement(By.xpath("//div//span[contains(text(),'Grid Report saved successfully.')]"));

   if(element1.isEmpty() )
   {
       //first set of code
   }
   else if(element2.isEmpty())
   {
       //second set of code
   }

second option has more processing so first one is more recommended

PDHide
  • 18,113
  • 2
  • 31
  • 46
0
  • Why don't you use the extension for convenience.
  • With chrome:
  • Using "Xpath helper" for check elements:

https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl?hl=en

  • Using "Xpath finder" for find elements:

https://chrome.google.com/webstore/detail/xpath-finder/ihnknokegkbpmofmafnkoadfjkhlogph?hl=en

Son Tran
  • 122
  • 1
  • 7
0

For your problem, do you already have the element located or you need to check if the element is visible after certain action?

  1. If you already has the element

Use method is_displayed from the element. For example:

# you have located element and assign it to elem
visibility = elem.is_displayed()
print(visibility) # will print True or False
  1. If you need to check if the element is visible after certain action

Use fluent wait, and wrap it with a try except.

from selenium.common.exceptions import TimeoutException

def check_visible_by_xpath(xpath, timeout=2):
    # Will actively search for the element in 2 seconds.
    # If found, return True and stop waiting.
    # If 2 seconds threshold reaches, return False (cannot find it visible in 2 seconds)
    try:
        element = WebDriverWait(driver, timeout).until(
            EC.visibility_of_element_located((By.XPATH, xpath)))
        return True
    except TimeoutException:
        return False



# ...
# Some actions here
type_in_phone_number()
# Now you want to check, if after your previous action, something pops up??

visibility = check_visible_by_xpath('//*[@data-animate-modal-popup="true"]', 2)
# check and do something about it
if visibility:
    action_if_visible()
else:
    action_if_not_visible()
jackblk
  • 1,076
  • 8
  • 19