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!!
CodePudding user response:
This element is inside an iframe. iframe inside iframe. So you have to switch to the inner iframe in order to access that element.
Something like this:
driver.get("https://www.gmx.com/consentpage")
wait = WebDriverWait(driver, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.permission-core-iframe")))
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[contains(@src,'plus')]")))
wait.until(EC.visibility_of_element_located((By.XPATH, '//button[@id="onetrust-accept-btn-handler"]'))).click()
CodePudding user response:
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:
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