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
Hi, I'm wondering why whenever a WebDriver method requires user to pass WebElement as a parameter, that a string WebElement ID is passed, instead of the WebElement object? The WebElement ID is only needed for passing on to Execute's data dictionary for Selenium WebDriver processing, and then in turn by ToWebElement to process Excecute's response. So it seems to me that use of the WebElement ID is mainly "under the hood" and should be invisible to the user.
It took me about 30 minutes of coding to make the transition from ID string to object for the entire code base. Here are some examples...
In WebDriver class:
' Returns element.value
Public Function GetValue(element As WebElement, Optional ByVal SessionId As String = vbNullString) As String
Dim Data As New Dictionary
If SessionId <> vbNullString Then
Data.Add "sessionId", SessionId
End If
Data.Add "id", element.ElementId
Data.Add "name", "value"
GetValue = Execute(CMD_GET_ELEMENT_ATTRIBUTE, Data)("value") 'see GCUser99's proposed changes to Execute
End Function
In WebElement Class using Me object to send reference to WebDriver class:
Private ElementId_ As String 'changed to private
Friend Property Let ElementId(ByVal val)
ElementId_ = val
End Property
Friend Property Get ElementId()
ElementId = ElementId_
End Property
' Returns element.value
Public Function GetValue() As String
GetValue = Driver_.GetValue(Me, SessionId_)
End Function
Here is how to handle an Optional WebElement argument in WebDriver class:
Public Function GetInnerHTML(Optional element As WebElement, Optional ByVal SessionId As String = vbNullString) As String
Dim Script As String
If element Is Nothing Then
Script = "return document.body.innerHTML"
Else
Script = "return arguments[0].innerHTML"
End If
GetInnerHTML = ExecuteScript(Script, vbNullString, element, SessionId)
End Function
And another Optional WebElement argument example:
' Find multiple DOM elements
Public Function FindElements(by_ As by, ByVal value As String, Optional parentElement As WebElement, 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
Dim cmd
If Not parentElement Is Nothing Then
Data.Add "id", parentElement.ElementId
cmd = CMD_FIND_CHILD_ELEMENTS
Else
cmd = CMD_FIND_ELEMENTS
End If
Dim elements
Set elements = Execute(cmd, Data)("value") 'see GCUser99's proposed changes to Execute
' To array of ids
Dim Ret() As WebElement
Dim i As Integer
For i = 0 To elements.Count - 1 ' elements is Collection, not array
ReDim Preserve Ret(i)
Set Ret(i) = ToWebElement(elements(i + 1)(ELEMENT_KEY), SessionId)
Next
' Return element ids
FindElements = Ret
End Function
I'm willing to share my version of the WebDriver and WebElement classes, if that is of interest. Thanks again for sharing your code on GitHub!
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
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I'm wondering why whenever a WebDriver method requires user to pass WebElement as a parameter, that a string WebElement ID is passed, instead of the WebElement object? The WebElement ID is only needed for passing on to Execute's data dictionary for Selenium WebDriver processing, and then in turn by ToWebElement to process Excecute's response. So it seems to me that use of the WebElement ID is mainly "under the hood" and should be invisible to the user.
It took me about 30 minutes of coding to make the transition from ID string to object for the entire code base. Here are some examples...
In WebDriver class:
In WebElement Class using Me object to send reference to WebDriver class:
Here is how to handle an Optional WebElement argument in WebDriver class:
And another Optional WebElement argument example:
I'm willing to share my version of the WebDriver and WebElement classes, if that is of interest. Thanks again for sharing your code on GitHub!
Beta Was this translation helpful? Give feedback.
All reactions