0

i have this code i wrote:

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

### Site options
options = Options()
options.add_argument("start-maximized")
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get("https://www.bosch-professional.com/bg/bg/")
time.sleep(5)

### Click Accept Cookies
driver.find_element(By.XPATH, "/html/body/dock-privacy-settings//div/modal-view/bbg-dialog/div/bbg-box/div/div/div/div[5]/span/span/p[3]/bbg-button/button/div").click()



Site is https://www.bosch-professional.com/bg/bg/, and i want to find and click on "Приемане На Всички"

enter image description here

I tried to click on "Приемане На Всички", but does not work. I receive error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/dock-privacy-settings//div/modal-view/bbg-dialog/div/bbg-box/div/div/div/div[5]/span/span/p[3]/bbg-button/button/div"}
ToniMi86
  • 1
  • 1

1 Answers1

0

The reason why the "accept cookies" button cannot be accessed directly in Selenium is because it is part of the shadow DOM. To interact with elements within the shadow DOM, you first need to obtain access to it and then search for the specific element you want to click.

enter image description here

I have tested Below and it works fine,It utilizes Selenium version 4 which now has added support for ShadowDOM. Previously, in version 3, the code to access elements within the shadow DOM was through javascript executor added as commented code.

shadow = driver.find_element(By.TAG_NAME, "dock-privacy-settings").shadow_root

Then once we have the ShadowDOM element we search inside it for accept button and do click

shadow.find_element(By.ID,
                    "save-all-modal-dialog").click()

Full Code

   import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

### Site options
options = Options()
options.add_argument("start-maximized")
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get("https://www.bosch-professional.com/bg/bg/")
time.sleep(10)
# For Version 3
# shadow = driver.execute_script (
#            "return arguments[0].shadowRoot", driver.find_element(By.TAG_NAME, "dock-privacy-settings"))

# For Version 4
shadow = driver.find_element(By.TAG_NAME, "dock-privacy-settings").shadow_root
### Click Accept Cookies
shadow.find_element(By.ID,
                    "save-all-modal-dialog").click()

ShadowDOM ShadowDOMintercaction

Abhay Chaudhary
  • 1,763
  • 1
  • 8
  • 13