0

I have problem of going through selenium exception: NoSuchElementException.

code example:

from selenium.common.exceptions import NoSuchElementException

try:
    popup = browser.find_element_by_xpath('//*)
    popup.click()
except NoSuchElementException:
    pass

with this i get crash on Django framework with above error type. What i need is to go through this exception and continue program. One option is to use:

except:
    pass

but i need to collect exceptions elements.

Overall element is found by browser but can't be clicked:

Message: element click intercepted: Element <a class="dropdown-toggle" data-toggle="dropdown" href="#" rel="nofollow">...</a> is not clickable at point (654, 570).

Thank You for any suggestions.

1
  • is there any errors that you are seeing? i suggest to add explicit wait for the element. Commented Nov 19, 2019 at 14:18

2 Answers 2

0

You can achieve that without try..catch. Check the length of the elements and if more that 0 then go to if block and execute else whatever you want to do.

if len(browser.find_elements_by_xpath('//*'))>0:
    popup =browser.find_element_by_xpath('//*')
    popup.click()
else:
    print("pass")

Or you can simply use if block.

if len(browser.find_elements_by_xpath('//*'))>0:
    popup =browser.find_element_by_xpath('//*')
    popup.click()

Edited:

Induce WebDriverWait and wait for element_to_be_clickable()

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

if len(browser.find_elements_by_xpath('//*'))>0:
    popup =WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,"//*")))
    popup.click()
else:
    print("pass")
Sign up to request clarification or add additional context in comments.

3 Comments

that will work but not in this case, because element is found but selenium can't click on it: Element is not clickable at point (654, 570)
@ZarakiKenpachi : Check my edited answer.Induce WebDriverWait and wait for element_to_be_clickable()
that will not always work, because in some cases clickable element was hidden behind popup window.
0

Solution was to add exception that will handle click error.

errors = []

try:
    popup = browser.find_element_by_xpath('//*')
    popup.click()
except NoSuchElementException as err:
    errors.append(err.msg)
    pass
except ElementNotInteractableException as err:
    errors.append(err.msg)
    pass

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.