Search code examples
python-3.xseleniumselenium-webdriverwebdrivernosuchelementexception

Py + SeleniumWebdriver - NoSuchElementException not working


I can't understand why isn't this working? I know that there is no such element on this page, but NoSuchElementException isn't raised!

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

driver = webdriver.Chrome("C:\\Users\\chromedriver.exe")
driver.get('https://www.kinopoisk.ru/')
try:
    driver.find_element_by_name('kp_query1')
except NoSuchElementException:
    print('WTF')
print('Ok!') 

Solution

  • A bit of more information about why you felt NoSuchElementException does't rises would have helped us to construct a more canonical answer.


    However, I took your code and when executed NoSuchElementException was properly caught and the except{} block was properly executed.

    • Code Block:

      driver.get('https://www.kinopoisk.ru/')
      try:
          driver.find_element_by_name('kp_query1')
      except NoSuchElementException:
          print('WTF')
      print('Ok!') 
      
    • Console Output:

      WTF
      Ok!