0

I am trying to use https://asunnot.oikotie.fi/myytavat-asunnot within iframe using Selenium in Python, but seem unable to locate the acceptance button for the cookie popup window.

Here's the code I built:

url = 'https://asunnot.oikotie.fi/myytavat-asunnot'
driver = webdriver.Safari()
driver.get(url)
driver.implicitly_wait(10)
button = driver.find_elements_by_class_name("button")
for ind, b in enumerate(button):
    print(ind, b.text.strip())
button[53].click()

Image: enter image description here

But I get the following error message:

selenium.common.exceptions.ElementNotInteractableException: Message: 

When I inspect the elements in the driver, it seems it contains the page in the background, but not the cookie popup window. I also tried, if it would be an alert, but it didn't seem to be.

Any ideas how to go forward?

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

1 Answers1

0

The element OK for cookie acceptance is within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

  • Induce WebDriverWait for the desired element to be clickable.

  • You can use either of the following Locator Strategies:

    • Using CSS_SELECTOR:

      driver.get("https://asunnot.oikotie.fi/myytavat-asunnot")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://cdn.privacy-mgmt.com/index']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='OK']"))).click()
      
    • Using XPATH:

      driver.get("https://asunnot.oikotie.fi/myytavat-asunnot")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@src, 'https://cdn.privacy-mgmt.com/index')]")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='OK']"))).click()
      
  • Note : You have to add the following imports :

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

Reference

You can find a couple of relevant discussions in:

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