Search code examples
pythonselenium-webdriverpopup

Selenium – can't click() on this site's pop-up button


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

browser = webdriver.Chrome()
browser.get('https://www.chefkoch.de/')
WebDriverWait(browser, 3).until(EC.element_to_be_clickable((By.XPATH,"//button[contains(text(),'Akzeptieren und weiter')]"))).click()

presence_of_element_located instead of clickable, by CSS_SELECTOR, the actual XPATH or the actual XPATH in it's full length, none of it works.

I just want to click() on the left button of that first appearing pop-up.

EDIT

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

browser = webdriver.Chrome()
browser.maximize_window()
browser.get('https://www.chefkoch.de/')

WebDriverWait(browser, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[@id='sp_message_iframe_847995']")))
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,"(//button[text()='Zustimmen'])[1]"))).click()
browser.switch_to.default_content()

WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="w-header"]/div[1]/div/div[2]'))).click()

So far so good... here comes the next overlay. This one does not appear to be an iframe, and again no matter what I've tried, can't interact with anything on/in this container

The input field is of interest to me

enter image description here


Solution

  • Assuming this is the button you want to click:

    enter image description here

    You need to switch into the IFRAME first before performing click() on the button. Refer the below working code:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait
    
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get('https://www.chefkoch.de/')
    
    # Switch into the IFRAME
    WebDriverWait(browser, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[@id='sp_message_iframe_847995']")))
    
    # Click on Zustimmen button
    WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,"(//button[text()='Zustimmen'])[1]"))).click()
    
    # Come out of IFRAME
    browser.switch_to.default_content()