1

My code does not throw NoSuchElementException even if no element found using WebDriverWait ..

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.common.exceptions import NoSuchElementException, TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
s = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s, options=options)
driver.get(r"https://www.google.com/")
invalid_xpath = "//h2"

time.sleep(2)

try:
    element = WebDriverWait(driver,20).until(ec.visibility_of_element_located((By.XPATH,invalid_xpath))) #ignored_exceptions=Nosuchelement does not work
    # element = driver.find_element(By.XPATH,invalid_xpath)
except NoSuchElementException:
    print("No such element exception occurs")
except TimeoutException:
    print("Timeout exception occurs")

The code element = driver.find_element(By.XPATH,invalid_xpath) works but I need to wait for the element to become visible as well.

fardV
  • 198
  • 9
  • 1
    A webdriverwait will ignore no such element exceptions... however it will throw a timeout after the timeoutperiod and in that exception you'd see what caused it... which would be no such element exception if that was the case. – pcalkins Mar 10 '23 at 21:23

0 Answers0