0

Im trying to access a shadow-root tag via Selenium Python and I can seem to reach it. Its a dynamic picklist created in salesforce and I know our current salesforce environment is using "Salesforce's Lighting Experience" so they are using a shadow DOM

Im trying to be able to pass a string to the <div>Unknown Place</div> which I believe is where someone would type if they were to do it normally.

<input type="text" autocomplete="off" tabindex="0" aria-expanded="true" aria-label="Search for your place below. If it is not listed, select &quot;Unknown Unknown.&quot;" aria-owns="ui-select-choices-0" class="form-control ui-select-search ng-pristine ng-valid ng-not-empty ng-touched" placeholder="Please enter 3 or more characters" ng-model="$select.search" ng-show="$select.searchEnabled &amp;&amp; $select.open" aria-activedescendant="ui-select-choices-row-0-0" style="width: 686px;">
#shadow-root (user-agent)
  <div pseudo="-webkit-input-placeholder" id="placeholder" style="display: none !important;">Please enter 3 or more characters</div>
  <div>Unknown Place</div>

Im using seleniums POM so I have the following.

import unittest
import os

import page
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

class Application(unittest.TestCase):

    def setUp(self):
        chromedrive_path = os.getenv("CHROMEDRIVER_PATH")
        service = Service(executable_path=chromedrive_path)
        self.driver = webdriver.Chrome(service=service)
        self.driver.implicitly_wait(10)
        self.driver.get("https://some.url.com")
        self.driver.maximize_window()


    def test_case_one(self):
        main_page.foo()


    def tearDown(self):
        self.driver.close()

if __name__ == "__main__":
    unittest.main()
import time
import os

from locator import *
from element import BasePageElement
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions

class Non_Shadow_Dom_Element(BasePageElement):
    locator = MainPageLocator.non_shadow_dom_element

class Shadow_Dom_Root_Element(BasePageElement):
    locator = MainPageLocator.shadow_dom_root_element

class BasePage():


    def __init__(self, driver):
        self.driver = driver


class MainPage(BasePage):

    def foo(self):
        # Locate the shadow host element
        shadow_host = self.driver.find_element(*MainPageLocator.non_shadow_dom_element)
        
        # This didnt work
        # shadow_root = shadowHost.getShadowRoot();

        # Access the shadow root
        shadow_root = driver.execute_script("return arguments[0].shadowRoot", shadow_host)

        
        # Find an element inside the shadow root
        shadow_element = shadow_root.find_element(*MainPageLocator.shadow_dom_root_element)
        print("Shadow Element Text:", shadow_element.text)

        time.sleep(5)
from selenium.webdriver.common.by import By


class MainPageLocator():
    non_shadow_dom_element = (By.CSS_SELECTOR, "input[placeholder='Please enter 3 or more characters']")
    shadow_dom_root_element = (By.CSS_SELECTOR, "input[placeholder='Please enter 3 or more characters']div") #Im not 100% sure how to reach this tag.

But all I keep getting as output is:

Shadow Root: None

I think the expected output is:

Shadow Root: Unknown Place

Python version: 3.13.2

Selenium version: 4.29.0

1 Answer 1

-1

#shadow-root (user-agent) can not be accessed as per this video.

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Mar 27 at 15:35

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.