0

I would like to click the button Alle akzeptieren. Unfortunately when I search in the print(browser.page_source) I don't find it. A WebDriverWait(browser, 10) did not help.

My code:

browser = webdriver.Firefox(executable_path='./geckodriver')

browser.get("https://www.finanzen.net/")
browser.find_element_by_xpath("//button[@class_='message-component message-button no-children button-responsive-primary']").click()

enter image description here

greetings from Ingolstadt

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
DK Germany
  • 11
  • 6

2 Answers2

1

This is because the button and the "popup" itself is inside the iframe:

frame

You'd need to switch to it prior to finding and clicking the button:

# switch to frame
frame = browser.find_element_by_css_selector("iframe[id^=sp_message_iframe]")
browser.switch_to.frame(frame)

# click the button
button = browser.find_element_by_css_selector("button.message-button.button-responsive-primary")
button.click()

# switch back to the default context
browser.switch_to.default_content()

Note that you may need to explicitly wait for the frame to appear to allow the page time to load and show the dialog in the frame:

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


browser = webdriver.Firefox(executable_path='./geckodriver')
browser.get("https://www.finanzen.net/")

wait = WebDriverWait(browser, 10)

# switch to frame
frame = wait.until(
    EC.visibility_of_element_located((By.CSS_SELECTOR, "iframe[id^=sp_message_iframe]"))
)
browser.switch_to.frame(frame)

# click the button
button = browser.find_element_by_css_selector("button.message-button.button-responsive-primary")
button.click()

# switch back to the default context
browser.switch_to.default_content()
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

The desired element Alle akzeptieren 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://www.finanzen.net/")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[id^='sp_message_iframe'][src^='https://consent.finanzen.net/index.html']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[title='Alle akzeptieren']"))).click()
      
    • Using XPATH:

      driver.get("https://www.finanzen.net/")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@id, 'sp_message_iframe') and starts-with(@src, 'https://consent.finanzen.net/index.html')]")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@title='Alle akzeptieren']"))).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