1

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
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
JimmyP1979
  • 13
  • 2

2 Answers2

0

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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

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)

Somebody Out There
  • 347
  • 1
  • 5
  • 15