1

I have been trying to do some things on this website with the Selenium Webdriver in Python, but every time the Webdriver opens the webpage, a cookie notification appears which I just cant close/bypass. I have already tried using cookie sessions, but that didnt work either. Here is my code (in which I attempted to use the cookies from my main browser):

from http import cookies
from time import sleep
from selenium import webdriver
from csv import DictReader

driver = webdriver.Firefox()
driver.get("https://www.antenne.de/programm/aktionen/pausenhof-konzerte/die-antenne-bayern-pausenhof-konzerte-2022/")

def get_cookies_values(file):
    with open(file, encoding="utf-8-sig") as f:
        dict_reader = DictReader(f)
        list_of_dicts = list(dict_reader)
    return list_of_dicts

cookies = get_cookies_values("cookies.csv")

for i in cookies:
    driver.add_cookie(i)

driver.refresh()

If anyone has any idea how to bypass/close the cookie notification or if you need any more information, let me know.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Maro
  • 5
  • 5

2 Answers2

1

The element Alle akzeptieren is within #shadow-root (open).

antenne_de


Solution

To click on Alle akzeptieren you have to use shadowRoot.querySelector() and you can use the following solution:

  • Code Block:

    driver.get('https://www.antenne.de/programm/aktionen/pausenhof-konzerte/die-antenne-bayern-pausenhof-konzerte-2022/')
    time.sleep(3)
    driver.execute_script("""return document.querySelector('#usercentrics-root').shadowRoot.querySelector("button[data-testid='uc-accept-all-button']")""").click()
    

Update

Using WebDriverWait:

driver.get('https://www.antenne.de/programm/aktionen/pausenhof-konzerte/die-antenne-bayern-pausenhof-konzerte-2022/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((driver.execute_script("""return document.querySelector('#usercentrics-root').shadowRoot.querySelector("button[data-testid='uc-accept-all-button']")""")))).click()

References

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you for your detailed solution! However I somehow still get an error: `Exception has occurred: AttributeError 'NoneType' object has no attribute 'click'` – Maro Jul 09 '22 at 05:21
  • Added a wait. Checkout the updated answer. – undetected Selenium Jul 09 '22 at 07:23
  • uh I have one more problem: I want to bypass a Captcha called "FriendlyCaptcha" (nothing special, you just have to wait) but if I select any school (example: [link](https://www.antenne.de/programm/aktionen/pausenhof-konzerte/schulen/oberbayern/grundschule-adelschlag/) ), click on "Jetzt abstimmen" then I dont know how to trigger the verification in selenium, I even tried clicking the button manually (in the Selenium browser session) but nothing happened. (if you open it with a normal browser it just starts the verification instantly. Any idea on how to "verify" myself? – Maro Jul 09 '22 at 08:24
  • Can you raise a new question with your new requirement please? – undetected Selenium Jul 09 '22 at 10:38
0

Have you tried closing out of the popup by having the webdriver find it by xpath and clicking on it?

accept_cookies = driver.find_element(By.XPATH, '/div/div/div/div[2]/div/div[2]/div/div[1]/div/button[2]')
accept_cookies.click()
a.b
  • 1
  • 1
  • 1
  • I tried running your code, but I got an error saying "name 'By' is not defined. Also, here is the content of the button (which is basically nothing and what makes it so impossible for me to find an solution: `` – Maro Jul 08 '22 at 18:42
  • @Maro Try `driver.find_element('css selector', 'button[data-testid="uc-accept-all-button"]').click()` and report what happens. If the button shows up a bit late after page load you may have to use selenium Wait and check for button element's availability before clicking on it. – user47 Jul 08 '22 at 18:46
  • @Firelord I used driver.implicitly_wait(10) so it waits, but even after the cookie page appeared nothing happened and I got this error `Exception has occurred: NoSuchElementException Message: Unable to locate element: button[data-testid="uc-accept-all-button"]` – Maro Jul 08 '22 at 18:57