0

I am trying to accept cookies on website, sadly I cannot refer to the "accept all" button. I have directly in the code time.sleep(10) so there should not be a problem with the wait on the popup/consent to be visible - which is something I mostly saw in similar threads.

driver.get("https://fischer.cz")
time.sleep(10)        
prijmoutVse = driver.find_element_by_xpath("//*[@class='sc-gtsrHT ctrzIr']")
driver.click(prijmoutVse)

I am getting selenium error that says it is unable to locate the element. I am unable to locate it with xpath helper in browser even when I copy the full xpath, all the results are null.

I tried driver.switch_to.alert as well but did not get this to working.

Tried following locators :

//*[@data-testid="uc-accept-all-button"]
/html/body/div[15]//section/div/div[2]/div[3]/div/div[1]/div/button[3]
//*[@id="uc-center-container"]/div[3]/div/div[1]/div/button[3]

code of the consent here is print of the element from console, I noticed there are some "flex" tags but not too sure what they mean or if it could cause trouble.

I am kinda desperate at this point since it blocks my work and I cant figure a way to work around it. I will be happy for any comments or thoughts how to solve that!

Thanks in advance

E1: tried the answer from cruisepandey getting this error

    Traceback (most recent call last):
  File "C:/Users/KDK/Desktop/NOVY SEARCH AUTO/test2.py", line 10, in <module>
    wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@data-testid='uc-accept-all-button']"))).click()
  File "C:\Users\KDK\anaconda3\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 


Process finished with exit code 1

E2: tried answer from pmadhu

Traceback (most recent call last):
  File "C:/Users/KDK/Desktop/NOVY SEARCH AUTO/test2.py", line 9, in <module>
    shadowroot.find_element_by_css_selector("#uc-center-container > div > div.sc-hiKfDv.gGhxCY > div > div.sc-ezzafa.bUHURR > div > button.sc-gtsrHT.ctrzIr").click()
  File "C:\Users\KDK\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 430, in find_element_by_css_selector
    return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
  File "C:\Users\KDK\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 658, in find_element
    return self._execute(Command.FIND_CHILD_ELEMENT,
  File "C:\Users\KDK\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\KDK\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\KDK\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"#uc-center-container > div > div.sc-hiKfDv.gGhxCY > div > div.sc-ezzafa.bUHURR > div > button.sc-gtsrHT.ctrzIr"}

2 Answers 2

1

The element is in a Shadow root

Try below code.

driver.get("https://www.fischer.cz/")
root1 = driver.find_element_by_id("usercentrics-root")
shadowroot = driver.execute_script("return arguments[0].shadowRoot", root1)
shadowroot.find_element_by_css_selector("#uc-center-container > div > div.sc-hiKfDv.gGhxCY > div > div.sc-ezzafa.bUHURR > div > button.sc-gtsrHT.ctrzIr").click()
Sign up to request clarification or add additional context in comments.

Comments

0

When the website is launched, I see Přijmout vše button, I think you wanna click on that.

try with below code :-

driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://fischer.cz/")
wait = WebDriverWait(driver, 20)

def expand_shadow_element(element):
  shadow_root = driver.execute_script('return arguments[0].shadowRoot', element)
  return shadow_root

outer = expand_shadow_element(driver.find_element_by_css_selector("div#usercentrics-root"))
inner = outer.find_element_by_css_selector("button[data-testid='uc-accept-all-button']")
inner.click()

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Comments

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.