2

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.

2 Answers 2

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:

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your detailed solution! However I somehow still get an error: Exception has occurred: AttributeError 'NoneType' object has no attribute 'click'
Added a wait. Checkout the updated answer.
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 ), 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?
Can you raise a new question with your new requirement please?
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()

3 Comments

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: <button role="button" data-testid="uc-accept-all-button" style="margin: 0px 6px;" class="sc-gsDKAQ cYtWkK">Alle akzeptieren</button>
@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.
@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"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.