Hi, I have a Python code to navigate through the ShadowDom but I don’t know how to code it in Robotframework. I got the first Shadow root element, but facing error in executing Javascript.
Python code :
def expand_shadow_element():
# return a list of elements
shadowRoot = driver.find_element(By.CSS_SELECTOR, "camos-html5client")
shadowRoot = driver.execute_script('return arguments[0].shadowRoot', shadowRoot)
executableShadow = shadowRoot.find_element(By.CSS_SELECTOR, "div#CHCArena")
return executableShadow
Robotframework code :
Expand Shadow Element
${shadowRoot}= Get WebElement css=camos-html5client
${shadowRoot}= Execute Javascript return arguments[0].shadowRoot ${shadowRoot}
${executableShadow}= Get WebElement css=div#CHCArena ${shadowRoot}
[Return] ${executableShadow}
One question:
Do you really need to use SeleniumLibrary?
Because Selenium 4 itself is bad with shadowDom and just the basic support for it and SeleniumLibrary has no features to support ShadowDom/WebComponents except of dom selector which is basically js.
Answer for ShadowRoot / ShadowDom / WebComponent automation is the playwright based Browser library.
Hi ramya, I have tried like this:
Use Shadow Path generator chrome extension and get the string value and by using js executor, click on the shadow dom and enter into it. see the reference code below:
FlaUILibrarycannot access shadow DOM directly, because it operates at the UI automation level (Windows native UI), not inside the browser’s HTML DOM.
Shadow DOM access must be handled via SeleniumLibrary (or Playwright) in Robot Framework, because these libraries can interact with web elements inside shadow DOMs.
this is with Python add it as a wrapper and use it in Robot Framework
This is for new browser
*** Test Cases ***
Click Deny Using JavaScript
Open Browser https://example.com Chrome
Execute JavaScript return document.querySelector(‘#usercentrics-cmp-ui’).shadowRoot.querySelector(‘#deny’).click();
Execute JavaScript ${script}
the below is for new Tab in a existing open browser
Open Url In New Tab And Wait
Execute JavaScript window.open(‘${New_Tab URL}’, ‘_blank’) # if new tab not open already this will open a new tab
Sleep 1s
Switch Window title=New_Tab Title name # Find in the Elements on clicking inspect take title tag
Sleep 2s
Execute javascript return document.querySelector(‘"IFrame id or tag that have the shadow Root encloed’).shadowRoot.querySelector('Button ID only CSS Selector id can be used ').click();
Sleep 2s
To Find Shadow DomElements use Only CSS in selector Hub inside Elements section.