Selenium TimeoutException when trying to click Cookies Agree

Viewed 170

I am trying to crawl some articles from a website and before doing so, I need to click the "Cookies Agree" using Selenium in Python.

But unfortunately, I keep getting either TimeoutException or NoSuchElementException!

I've figured out that the click button is within iframe, so I've switched to it and clicked the consent button.

homepage = 'link'
driver.maximize_window()
driver.get(homepage)
driver.implicitly_wait(5)

driver.switch_to.frame('location')

try:
  consent = wait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, 'classname')))
  consent.click()
except TimeoutException :
  print('timeoutexception')

driver.switch_to.default_content()

iframe

consent click button

But still I just can't get through the TimeoutException error.

What have I done wrong....?!

1 Answers

You mess up wait and driver objects. They are different. Switch to iframe and wait for your button.

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


#  switch to frame here

wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable(
                (By.CSS_SELECTOR, '.message-component.message-button.no-children:nth-of-type(2)')))
consent = driver.find_element_by_css_selector('.message-component.message-button.no-children:nth-of-type(2)')
consent.click()

There are two .message-component.message-button.no-children css locators and you need the second one.

To find iframe use (this is bulletproof):

driver.switch_to.frame(driver.find_element_by_xpath("//iframe[contains(@id,'sp_message_iframe_')]"))
Related