简体   繁体   中英

How to click a button with Selenium that don't exist at browser.page_source

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()

在此处输入图像描述

greetings from Ingolstadt

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

框架

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()

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM