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?