If Statements with Selenium using find element by xpath (contains text)

Viewed 215

I am trying to find an element by text and depending if it is found or not print an output to the screen

This is what I have so far but I just cant get it to work

if driver.find_element_by_xpath("//*[contains(text(),'addFromWishlist')]").isEmpty()
{
System.out.println("In stock");
}
else{
  System.out.println("Not In Stock");
}
1 Answers

Please use find_elements (plural) so that it would return a list of web element if found, if not it will return empty list. either way there won't be any exception.

try:
    if len(driver.find_elements(By.XPATH, "//*[contains(text(),'addFromWishlist')]")) >0 :
        print('In stock')
    else:
        print('Not In Stock')
except:
    print('Something went wrong')
    pass
Related