i try to find a shadow root on a website and clicking a button using the following code:
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
print(f"Checking Browser driver...")
options = Options()
options.add_argument("start-maximized")
options.add_argument('--log-level=3')
options.add_experimental_option("prefs", {"profile.default_content_setting_values.notifications": 1})
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
srv=Service()
driver = webdriver.Chrome (service=srv, options=options)
link = "https://www.arbeitsagentur.de/jobsuche/suche?wo=Berlin&angebotsart=1&was=Gastronomie%20-%20Minijob&umkreis=50"
driver.get (link)
time.sleep(5)
shadowHost = driver.find_element(By.XPATH,'//bahf-cd-modal[@class="modal-open sc-bahf-cd-modal-h sc-bahf-cd-modal-s hydrated"]')
shadowRoot = shadowHost.shadow_root
shadowRoot.find_element(By.CSS_SELECTOR, "button[data-testid='bahf-cookie-disclaimer-btn-alle']").click()
input("Press!")
But i allways get this error:
(selenium) C:\DEVNEU\Fiverr2025\TRY\hedifeki>python test.py
Checking Browser driver...
Press
Traceback (most recent call last):
File "C:\DEVNEU\Fiverr2025\TRY\hedifeki\test.py", line 26, in <module>
shadowHost = driver.find_element(By.XPATH,'//bahf-cd-modal[@class="modal-open sc-bahf-cd-modal-h sc-bahf-cd-modal-s hydrated"]')
File "C:\DEVNEU\.venv\selenium\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 770, in find_element
return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\DEVNEU\.venv\selenium\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 384, in execute
self.error_handler.check_response(response)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
File "C:\DEVNEU\.venv\selenium\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 232, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//bahf-cd-modal[@class="modal-open sc-bahf-cd-modal-h sc-bahf-cd-modal-s hydrated"]"}
(Session info: chrome=136.0.7103.94); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
Stacktrace:
GetHandleVerifier [0x00007FF732A4CF65+75717]
GetHandleVerifier [0x00007FF732A4CFC0+75808]
How can i click the button in this shadow root?
'//bahf-cd-modal[@class="modal-open sc-bahf-cd-modal-h sc-bahf-cd-modal-s hydrated"]'
is for an element that is inside the shadow root, you need to locate an element that contains the shadow root
shadow_root = driver.find_element(By.TAG_NAME, 'bahf-cookie-disclaimer-dpl3').shadow_root
shadow_root.find_element(By.CSS_SELECTOR, "button[data-testid='bahf-cookie-disclaimer-btn-alle']").click()