I try to click on 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.support import expected_conditions as EC
from selenium.webdriver.common.by import By
if __name__ == '__main__':
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)
waitWD = WebDriverWait (driver, 10)
link = "https://www.arbeitsagentur.de/jobsuche/jobdetail/10001-1000341861-S"
driver.get (link)
time.sleep(3)
waitWD.until(EC.element_to_be_clickable((By.XPATH, '//button[@class="ba-btn ba-btn-primary"]'))).click()
But when i run this code i get the following error -
(selenium) C:\DEV\Fiverr2024\TRY\ben_hypace>python temp1.py
Checking Browser driver...
Traceback (most recent call last):
File "C:\DEV\Fiverr2024\TRY\ben_hypace\temp1.py", line 28, in <module>
waitWD.until(EC.element_to_be_clickable((By.XPATH, '//button[@class="ba-btn ba-btn-primary"]'))).click()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\DEV\.venv\selenium\Lib\site-packages\selenium\webdriver\support\wait.py", line 105, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Why is this red button "Alles zulassen" not clicked on this site using selenium?
Why is this red button "Alles zulassen" not clicked on this site using selenium?
Answer to above is because the targeted element is within shadow-root element.
Refer below code to handle clicks
within shadow-root
elements:
shadow_host = waitWD.until(EC.presence_of_element_located((By.TAG_NAME, 'bahf-cookie-disclaimer-dpl3')))
shadow_root = shadow_host.shadow_root
cookie_button = WebDriverWait(shadow_root, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.ba-btn.ba-btn-primary")))
driver.execute_script("arguments[0].click();", cookie_button)
References - https://stackoverflow.com/a/78471405/7598774