Selenium Python - Handling No such element exception
Asked Answered
T

5

39

I am writing automation test in Selenium using Python. One element may or may not be present. I am trying to handle it with below code, it works when element is present. But script fails when element is not present, I want to continue to next statement if element is not present.

try:
       elem = driver.find_element_by_xpath(".//*[@id='SORM_TB_ACTION0']")
       elem.click()
except nosuchelementexception:
       pass

Error -

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element:{"method":"xpath","selector":".//*[@id='SORM_TB_ACTION0']"}
Thermistor answered 24/6, 2016 at 21:56 Comment(0)
H
28

You can see if the element exists and then click it if it does. No need for exceptions. Note the plural "s" in .find_elements_*.

elem = driver.find_elements_by_xpath(".//*[@id='SORM_TB_ACTION0']")
if len(elem) > 0
    elem[0].click()
Hydrogenous answered 24/6, 2016 at 23:11 Comment(2)
Isn't it much worse in terms of performance? I believe it's much easier to find an element by id rather than by xpath.Giro
@SergeRogatch Agreed 100% but that's what OP had in their code so I replicated it. This is also 4 years ago and I like to think I've learned a lot since then and am more apt to bring up points like yours now in addition to "just answering the question".Hydrogenous
E
88

Are you not importing the exception?

from selenium.common.exceptions import NoSuchElementException

try:
    elem = driver.find_element_by_xpath(".//*[@id='SORM_TB_ACTION0']")
    elem.click()
except NoSuchElementException:  #spelling error making this code not work as expected
    pass
Epitaph answered 24/6, 2016 at 22:4 Comment(0)
H
28

You can see if the element exists and then click it if it does. No need for exceptions. Note the plural "s" in .find_elements_*.

elem = driver.find_elements_by_xpath(".//*[@id='SORM_TB_ACTION0']")
if len(elem) > 0
    elem[0].click()
Hydrogenous answered 24/6, 2016 at 23:11 Comment(2)
Isn't it much worse in terms of performance? I believe it's much easier to find an element by id rather than by xpath.Giro
@SergeRogatch Agreed 100% but that's what OP had in their code so I replicated it. This is also 4 years ago and I like to think I've learned a lot since then and am more apt to bring up points like yours now in addition to "just answering the question".Hydrogenous
L
14

the way you are doing it is fine.. you are just trying to catch the wrong exception. It is named NoSuchElementException not nosuchelementexception

Labonte answered 22/5, 2017 at 19:38 Comment(0)
I
2

Handling Selenium NoSuchExpressionException

from selenium.common.exceptions import NoSuchElementException
try:
   elem = driver.find_element_by_xpath
   candidate_Name = j.find_element_by_xpath('.//span[@aria-hidden="true"]').text
except NoSuchElementException:
       try:
          candidate_Name = j.find_element_by_xpath('.//a[@class="app-aware link"]').text
       except NoSuchElementException:
              candidate_Name = "NAN"
              pass
Ionize answered 13/1, 2022 at 20:34 Comment(0)
P
-2

Why not simplify and use logic like this? No exceptions needed.

if elem.is_displayed():
    elem.click()
Prestigious answered 24/6, 2016 at 22:26 Comment(1)
This will throw if the element is not found.Hydrogenous

© 2022 - 2024 — McMap. All rights reserved.