admin管理员组文章数量:1399490
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 "Unknown Unknown."" 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 && $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(";)
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.webdrivermon.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.webdrivermon.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
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 "Unknown Unknown."" 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 && $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")
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.webdrivermon.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.webdrivermon.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
Share Improve this question asked Mar 26 at 19:03 user6315807user6315807 151 silver badge5 bronze badges1 Answer
Reset to default -1#shadow-root (user-agent) can not be accessed as per this video.
本文标签: How to access shadowroot via Selenium PythonStack Overflow
版权声明:本文标题:How to access shadow-root via Selenium Python - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744130971a2592177.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论