franklin
franklin

Reputation: 1819

Verify element existence in Selenium

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?

Upvotes: 0

Views: 585

Answers (1)

Louis
Louis

Reputation: 151511

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.

Upvotes: 2

Related Questions