0

Some sites has age limitation, which will ask you whether you are over eighteen years old and you click yes to enter the site or no no to close the page

Here are snippet of code

driver.get(url2)
if (len(driver.find_elements_by_class_name('certification_layout_01'))==1):
   ask_button = driver.find_element_by_css_selector('dd.yes a')
   ask_button.click()

if selenium find the class certification_layout_01 then find go further to find anchor link and click to enter the site.

I found that if driver cannot find that class name the page just hold for a period before proceed the remaining statements after the if statement. I felt that it was waiting for a timeout before proceed.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Adrian Mak
  • 21
  • 2
  • When you run `driver.get(url)`, selenium will wait until your page has been loaded (equivalent to javascript's `document.readyState === "complete"`). Then it will execute your remain script as you mentioned. – Minh Dao Jan 31 '20 at 04:48
  • Are you using an implicit wait? If so, that's probably it. – JeffC Jan 31 '20 at 05:46

1 Answers1

0

There can be multiple aspects behind Selenium being slow to find a non-existing element (class) as follows:

  • I would suspect you have set the ImplicitWait which gets baked in to the WebDriver instance.
  • You may opt either of the following:
  • You may like to look out for the Yes directly to invoke click() and refactor your code as follows:

    driver.get(url2)
    try:
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "dd.yes a"))).click()
        print("Clicked on Yes button")
    except TimeoutException:
        print("Yes button wasn't found")
        break
    

tl; dr

What is the default wait time of the selenium webdriver before throwing NoSuchElementException

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352