2

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....?!

MongMong
  • 31
  • 6

1 Answers1

1

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_')]"))
vitaliis
  • 4,082
  • 5
  • 18
  • 40
  • Oh, wow! I had no idea that I was messing up two different objects. People seemed to use them interchangeably, so I just assumed that i could use it like that. Thank you! – MongMong Apr 03 '21 at 03:38
  • Yes, of course! But could you help me with the "NoSuchFrameException". After correcting the code, I get "NoSuchFrameException: Message: sp_message_iframe_216133". I didn't get such error before... – MongMong Apr 03 '21 at 04:07
  • Ok, the locator for the frame is dynamic. I'll help you with it – vitaliis Apr 03 '21 at 04:09
  • And is there any possibility that there's a syntax error in "wait = WebDriverWait(driver, 20)"? I must have done something wrong, because suddenly I get syntax error... – MongMong Apr 03 '21 at 04:33
  • Take this as a template: wait = WebDriverWait(driver, 20) wait.until(EC.element_to_be_clickable( (SelectBy.CSS_SELECTOR, "your_css"))) – vitaliis Apr 03 '21 at 04:36
  • Be careful with indents. Also check here https://selenium-python.readthedocs.io/waits.html#:~:text=Selenium%20Webdriver%20provides%20two%20types,trying%20to%20locate%20an%20element. – vitaliis Apr 03 '21 at 04:37
  • Try to wait for iframe first. Then - for your button. It has to work – vitaliis Apr 03 '21 at 04:39
  • For debugging use time.sleep(some amount is seconds) – vitaliis Apr 03 '21 at 04:40