Home > Net >  Try to click the "accept-cookies" button Error: "no such element: Unable to locate el
Try to click the "accept-cookies" button Error: "no such element: Unable to locate el

Time:05-04

i want to click the "accept Cookies" Button, but everytime i get the following error "Message: no such element: Unable to locate element:". I think this is because this is a popup-window. I try to click the Button via Xpath.

How can i handle this?

Picture of the Website

My Code:

from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome('./chromedriver')
link = 'https://www.ticket-onlineshop.com/ols/fckoeln/de/heim/channel/shop/index/index/event/424413'

#Open Website
driver.get(link)
#Timeout
time.sleep(5)
#Cookies
driver.find_element(By.XPATH,'//*[@id="uc-center-container"]/div[2]/div/div/div/div[2]/button[2]').click() 
Error:
  driver = webdriver.Chrome('./chromedriver')
Traceback (most recent call last):
  File "C:\Users\Manu\PycharmProjects\SampleSale\FC.py", line 12, in <module>
    driver.find_element(By.XPATH, '//*[@id="uc-center-container"]/div[2]/div/div/div/div[2]/button[2]').click()
  File "C:\Users\Manu\PycharmProjects\SampleSale\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1248, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Users\Manu\PycharmProjects\SampleSale\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 425, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Manu\PycharmProjects\SampleSale\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="uc-center-container"]/div[2]/div/div/div/div[2]/button[2]"}
  (Session info: chrome=101.0.4951.41)
Stacktrace:
Backtrace:
    Ordinal0 [0x0067B8F3 2406643]
    Ordinal0 [0x0060AF31 1945393]
    Ordinal0 [0x004FC748 837448]
    Ordinal0 [0x005292E0 1020640]
    Ordinal0 [0x0052957B 1021307]
    Ordinal0 [0x00556372 1205106]
    Ordinal0 [0x005442C4 1131204]
    Ordinal0 [0x00554682 1197698]
    Ordinal0 [0x00544096 1130646]
    Ordinal0 [0x0051E636 976438]
    Ordinal0 [0x0051F546 980294]
    GetHandleVerifier [0x008E9612 2498066]
    GetHandleVerifier [0x008DC920 2445600]
    GetHandleVerifier [0x00714F2A 579370]
    GetHandleVerifier [0x00713D36 574774]
    Ordinal0 [0x00611C0B 1973259]
    Ordinal0 [0x00616688 1992328]
    Ordinal0 [0x00616775 1992565]
    Ordinal0 [0x0061F8D1 2029777]
    BaseThreadInitThunk [0x761D6739 25]
    RtlGetFullPathName_UEx [0x77C78E7F 1215]
    RtlGetFullPathName_UEx [0x77C78E4D 1165]

Process finished with exit code 1

CodePudding user response:

Try:


driver.find_element(By.XPATH,”//*[@id=‘uc-center-container’]/div[2]/div/div/div/div[2]/button[2]”).click() 

CodePudding user response:

The element is present in the shadow root, to click the button we need to use querySelector() ..... with the below code i can able to click the button

driver.execute_script('''return document.querySelector('div#usercentrics-root').shadowRoot.querySelector('button[data-testid="uc-accept-all-button"]')''').click()

it is already explained in one of the StackOverflow question Link

  • Related