You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
How to handle Shadow Root DOM? The Selenium Webdriver does have functionality to handle. Below are the roughed-out basics to get started...
Please Note: Below I'm using the coding styles that I discuss in The Execute Method Issue and The WebElement ID Issue discussions. But if you love dealing with WebElement ID strings instead, then it would not be difficult to convert the code below back to that style. 🙂
First, we need to add to the COMMAND declaration and initialization lists in the WebDriver class:
Public CMD_GET_ELEMENT_SHADOW_ROOT
Public CMD_FIND_ELEMENT_FROM_SHADOW_ROOT
Public CMD_FIND_ELEMENTS_FROM_SHADOW_ROOT
Private Driver_ As WebDriver
Private SessionId_ As String
Private ShadowRootId_ As String
Friend Property Set Driver(objWebDriver As WebDriver)
Set Driver_ = objWebDriver
End Property
Friend Property Let sessionId(ByVal val)
SessionId_ = val
End Property
Friend Property Let ShadowRootId(ByVal val)
ShadowRootId_ = val
End Property
Friend Property Get ShadowRootId()
ShadowRootId = ShadowRootId_
End Property
'Find DOM element
Public Function FindElement(by_ As by, ByVal value As String) As WebElement
Set FindElement = Driver_.FindElementFromShadowRoot(by_, value, Me, SessionId_)
End Function
'Find multiple DOM elements
Public Function FindElements(by_ As by, ByVal value As String) As WebElement()
FindElements = Driver_.FindElementsFromShadowRoot(by_, value, Me, SessionId_)
End Function
Add the following to the WebDriver class:
Public Function GetShadowRoot(element As WebElement, Optional ByVal sessionId As String = vbNullString) As ShadowRoot
Dim data As New Dictionary, resp As Dictionary
If sessionId <> vbNullString Then
data.Add "sessionId", sessionId
End If
data.Add "id", element.elementId
Set resp = Execute(CMD_GET_ELEMENT_SHADOW_ROOT, data)
Set GetShadowRoot = ToShadowRoot(resp("value")(SHADOW_KEY), sessionId)
End Function
Public Function FindElementFromShadowRoot(by_ As by, ByVal value As String, shadowroot As ShadowRoot, Optional ByVal sessionId As String = vbNullString) As WebElement
Dim data As Dictionary
Set data = ToSelector(by_, value)
If sessionId <> vbNullString Then
data.Add "sessionId", sessionId
End If
data.Add "sid", shadowroot.ShadowRootId
' Return element
Set FindElementFromShadowRoot = ToWebElement(Execute(CMD_FIND_ELEMENT_FROM_SHADOW_ROOT, data)("value")(ELEMENT_KEY), sessionId)
End Function
Public Function FindElementsFromShadowRoot(by_ As by, ByVal value As String, shadowroot As ShadowRoot, Optional ByVal sessionId As String = vbNullString) As WebElement()
Dim data As Dictionary, elements
Set data = ToSelector(by_, value)
If sessionId <> vbNullString Then
data.Add "sessionId", sessionId
End If
data.Add "sid", shadowroot.ShadowRootId
Set elements = Execute(CMD_FIND_ELEMENT_FROM_SHADOW_ROOT, data)("value")
' To array of ids
Dim Ret() As WebElement
Dim i As Integer
For i = 0 To elements.Count - 1
ReDim Preserve Ret(i)
Set Ret(i) = ToWebElement(elements(i + 1)(ELEMENT_KEY), sessionId)
Next
' Return element ids
FindElementsFromShadowRoot = Ret
End Function
Private Function ToShadowRoot(ByVal ShadowRootId As String, Optional ByVal sessionId As String = vbNullString) As ShadowRoot
Dim sr As New ShadowRoot
Set sr.Driver = Me
If sessionId = vbNullString Then
sr.sessionId = sessionId
Else
sr.sessionId = DefaultSessionId
End If
sr.ShadowRootId = ShadowRootId
Set ToShadowRoot = sr
End Function
And finally, add this to the WebElement class:
Public Function GetShadowRoot(Optional ByVal sessionId As String = vbNullString) As ShadowRoot
Set GetShadowRoot = Driver_.GetShadowRoot(Me, SessionId_)
End Function
Below is a test usage case:
Sub test_shadow_roots_clear_browser_history()
Dim Driver As New WebDriver
Dim webelem1 As WebElement, webelem2 As WebElement, webelem3 As WebElement
Dim webelem4 As WebElement, webelem5 As WebElement, webelem6 As WebElement
Dim clearData As WebElement
Driver.Chrome 'this is a Chrome-only test example
Driver.OpenBrowser
'make some browsing history
Driver.Navigate "https://www.google.com/"
Driver.Wait 1000
'now go to chrome browser settings
Driver.Navigate "chrome://settings/clearBrowserData/"
Driver.Wait 1000
'traverse down the nested shadow root tree until we find the clear history button
Set webelem1 = Driver.FindElement(by.CssSelector, "settings-ui")
Set webelem2 = webelem1.GetShadowRoot.FindElement(by.CssSelector, "settings-main")
Set webelem3 = webelem2.GetShadowRoot.FindElement(by.CssSelector, "settings-basic-page")
Set webelem4 = webelem3.GetShadowRoot.FindElement(by.CssSelector, "settings-section > settings-privacy-page")
Set webelem5 = webelem4.GetShadowRoot.FindElement(by.CssSelector, "settings-clear-browsing-data-dialog")
Set webelem6 = webelem5.GetShadowRoot.FindElement(by.CssSelector, "#clearBrowsingDataDialog")
Set clearData = webelem6.FindElement(by.CssSelector, "#clearBrowsingDataConfirm")
clearData.Click 'to clear browsing history
Driver.Wait 3000
Driver.CloseBrowser
Driver.Shutdown
End Sub
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
How to handle Shadow Root DOM? The Selenium Webdriver does have functionality to handle. Below are the roughed-out basics to get started...
Please Note: Below I'm using the coding styles that I discuss in The Execute Method Issue and The WebElement ID Issue discussions. But if you love dealing with WebElement ID strings instead, then it would not be difficult to convert the code below back to that style. 🙂
First, we need to add to the COMMAND declaration and initialization lists in the WebDriver class:
In the initCommands Sub:
Then add the following ShadowRoot class:
Add the following to the WebDriver class:
And finally, add this to the WebElement class:
Below is a test usage case:
Beta Was this translation helpful? Give feedback.
All reactions