简体   繁体   中英

Python and Selenium: Locating and clicking a button for cookies within an iframe

Anybody has a solution to locate a button in webpage with an overlayed popup window like in the following example:

from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'./geckodriver')
driver.get("https://www.academics.de/")
#after waiting for a while the popup window comes up
driver.find_elements_by_xpath("//*[contains(text(), 'Zustimmen')]")

The returned list is empty. Running the following

driver.find_element_by_css_selector(".button-accept")

results in:

NoSuchElementException: Message: Unable to locate element: .button-accept

This may be helpful.

executable_path = r"C:\\Users\\Selenium+Python\\geckodriver.exe"
options.add_experimental_option("prefs", {"profile.default_content_setting_values.notifications": 2})
options.add_experimental_option("prefs", {"profile.default_content_settings.cookies": 2})
options.add_argument("start-maximized")
driver = webdriver.Chrome(executable_path, options=options)

The element with the text as E-Mail Login 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.academics.de/") WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='SP Consent Message']"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[title='Zustimmen']"))).click()
    • Using XPATH :

       driver.get("https://www.academics.de/") WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@title='SP Consent Message']"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@title='Zustimmen']"))).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:

An easy workaround to your problem would be to use uBlock Origin extension + Fanboy's Annoyances blocklist on your Selenium instance so that these annoying cookies messages would outright never appear. A way of enabling extensions is described in this StackOverflow answer :

  1. Create a new firefox profile via right click windows start button > run > firefox.exe -P
  2. Then add whatever extensions you want, ublock, adblock plus etc
  3. Call your profile folder with

profile = selenium.webdriver.FirefoxProfile("C:/test")

browser = selenium.webdriver.Firefox(firefox_profile=profile, options=ops)

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