PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, April 15, 2022

[FIXED] How do I handle cookie accept button within nested iframes in selenium with python?

 April 15, 2022     css-selectors, iframe, python, selenium, xpath     No comments   

Issue

I've tried almost everything and searched on SO but can't get passed the cookie accept on gmx.com. Hoping someone can help out. So far I've tried:

driver = webdriver.Chrome(CHROMEPATH)
driver.get('https://www.gmx.com')
time.sleep(5)
cookie_accept = WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, '//button[@id="onetrust-accept-btn-handler"]')))
cookie_accept.click()

===AND===

driver = webdriver.Chrome(CHROMEPATH)
driver.get('https://www.gmx.com')
wait = WebDriverWait(driver, 15)
wait.until(EC.element_to_be_clickable(
    (By.CSS_SELECTOR, "div[style='onetrust-style'] button[id*='onetrust-accept-btn-handler']")))
driver.find_element_by_css_selector("div[style='onetrust-style'] button[id*='onetrust-accept-btn-handler']").click()
time.sleep(10)
driver.quit()

What am I doing wrong?! Any help is greatly appreciated!!


Solution

The element Agree and continue is within nested elements so you have to:

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

  • Induce WebDriverWait for the child 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://www.gmx.com/consentpage")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.permission-core-iframe")))
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://plus.gmx.com/lt']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
      
    • Using XPATH:

      driver.get("https://www.gmx.com/consentpage")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@class='permission-core-iframe']")))
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@src, 'https://plus.gmx.com/lt')]")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='onetrust-accept-btn-handler']"))).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
    
  • Browser Snapshot:

GMX


Reference

You can find a couple of relevant discussions in:

  • Ways to deal with #document under iframe
  • Switch to an iframe through Selenium and python
  • selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
  • selenium in python : NoSuchElementException: Message: no such element: Unable to locate element


Answered By - undetected Selenium
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing