Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python selenium webscraping "NoSuchElementException" not recognized

Sometimes on a page I'll be looking for an element which may or may not be there. I wanted to try/catch this case with a NoSuchElementException, which selenium was throwing when certain HTML elements didn't exist. Original exception:

NoSuchElementException: Message: u'Unable to locate element: {"method":"css selector","selector":"#one"}' ; Stacktrace:      at FirefoxDriver.prototype.findElementInternal_ (file:///var/folders/6q/7xcjtgyj32nfc2yp_y5tr9pm0000gn/T/tmp63Mz2a/extensions/[email protected]/components/driver_component.js:8899)     at FirefoxDriver.prototype.findChildElement (file:///var/folders/6q/7xcjtgyj32nfc2yp_y5tr9pm0000gn/T/tmp63Mz2a/extensions/[email protected]/components/driver_component.js:8911)     at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/6q/7xcjtgyj32nfc2yp_y5tr9pm0000gn/T/tmp63Mz2a/extensions/[email protected]/components/command_processor.js:10840)     at DelayedCommand.prototype.executeInternal_ (file:///var/folders/6q/7xcjtgyj32nfc2yp_y5tr9pm0000gn/T/tmp63Mz2a/extensions/[email protected]/components/command_processor.js:10845)     at DelayedCommand.prototype.execute/< (file:///var/folders/6q/7xcjtgyj32nfc2yp_y5tr9pm0000gn/T/tmp63Mz2a/extensions/[email protected]/components/command_processor.js:10787)  

Ironically, it won't let me catch this exception which it was throwing before? Code here:

elt = driver.find_element_by_css_selector('.information') try:     dat1 = elt.find_element_by_css_selector('#one').text     dat2 = elt.find_elements_by_css_selector('#two')[1].text     text = dat1 + dat2 except NoSuchElementException:     text = elt.find_element_by_css_selector('#all').text     item.set_description(text) 

Error here:

NameError: name 'NoSuchElementException' is not defined 

Googling/documentation came up with nothing...and it strikes me as strange that selenium is fine throwing an exception but can't catch it.

like image 515
lollercoaster Avatar asked Oct 05 '13 17:10

lollercoaster


People also ask

What is NoSuchElementException selenium Python?

When we try to run the code below, it raises NoSuchElementException . This is because we try to find an element called userNam , but the webpage element name in the source code is userName . 20. 1. from selenium import webdriver.

Why do we get NoSuchElementException in selenium?

NoSuchElementException is one of the different WebDriver Exceptions and this Exception occurs, when the locators (i.e. id / xpath/ css selectors etc) we mentioned in the Selenium Program code is unable to find the web element on the web page.


1 Answers

You need to import the exception first

from selenium.common.exceptions import NoSuchElementException 

and then you can reference it

except NoSuchElementException:     # handle the element not existing 

If you would like to give details of the exception in your output then you can use:

except NoSuchElementException as exc:     print(exc) # and/or other actions to recover  
like image 119
Steve Barnes Avatar answered Sep 18 '22 09:09

Steve Barnes