Verify element existence in Selenium

Question:

I can’t figure out how to do this even after looking at the API. I want to scrape data over the web and I have the following code:

browser = webdriver.Firefox()
browser.get(url)
browser.find_element_by_xpath('//path')

Now before the find_element part executes. I want to know if the element in fact exists. I’ve tried the following:

try:
    browser.find_element_by_xpath('//path')
except NoSuchElementException:
    print ('failure')

And

if (browser.find_element_by_xpath('//path')[0].size() == 0):
    print ('failure')

But neither of them work, even though the API says size() is a callable function of WebElement and the exception is throw by the find family of methods. I don’t understand why neither of those work. What is
the explanation and what would be a working example?

Asked By: franklin

||

Answers:

Your first code snippet should raise NoSuchElementException if the element is missing. If it does not do that, then it is unclear from the information in your question why that’s the case. (Or maybe you expect it to not find an element but it does find one?)

The 2nd snippet cannot work because the find_element_... methods return WebElement objects, not lists of objects. You’d have to do:

if len(browser.find_elements_by_xpath('//path')) == 0:
    print ('failure')

Note the plural find_elements_... The functions to find elements that are in the plural return lists, and if they don’t find anything the list is of length zero.

By the way, the size() method on WebElement objects returns the rendered size of the element, that is, its height and width on the page.

Answered By: Louis
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.