TinySeleniumVBA
TinySeleniumVBA copied to clipboard
Add Method. GetAttribute, GetInnerHTML, GetOuterHTML
Add Method. GetAttribute, GetInnerHTML, GetOuterHTML #34
GetAttribute:要素の属性名から属性値を取得する。 GetInnerHTML:要素のinnerHTMLを取得する。 GetOuterHTML:要素のouterHTMLを取得する。 TinySeleniumVBA WebDriver.cls
' Get Attribute '2021/6/25 add ishi -> 2021/07/20 chg ishi -> 2021/9/30 chg ishi -> 2021/10/24 chg ishi
Public Function GetAttribute(value As String, _
ElementId As String, _
Optional ByVal sessionId As String = vbNullString) As String
Dim Script As String
If value = "innerHTML" Then
Script = "return arguments[0].innerHTML"
ElseIf value = "outerHTML" Then
Script = "return arguments[0].outerHTML"
Else
Script = "return arguments[0].getAttribute('" & value & "')"
End If
GetAttribute = ExecuteScript(Script, vbNullString, ElementId, sessionId)
End Function
' Get innerHTML '2021/6/30 add ishi -> 2021/07/20 chg ishi -> 2021/9/30 chg ishi -> 2021/10/24 chg ishi
Public Function GetInnerHTML(ElementId As String, _
Optional ByVal sessionId As String = vbNullString) As String
Dim Script As String
Script = "return arguments[0].innerHTML"
GetInnerHTML = ExecuteScript(Script, vbNullString, ElementId, sessionId)
End Function
' Get outerHTML '2021/6/30 add ishi -> 2021/07/20 chg ishi -> 2021/9/30 chg ishi -> 2021/10/24 chg ishi
Public Function GetOuterHTML(ElementId As String, _
Optional ByVal sessionId As String = vbNullString) As String
Dim Script As String
Script = "return arguments[0].outerHTML"
GetOuterHTML = ExecuteScript(Script, vbNullString, ElementId, sessionId)
End Function
TinySeleniumVBA WebElement.cls
' Returns element.attribute '2021/6/25 add ishi
Public Function GetAttribute(value As String) As String
GetAttribute = Driver_.GetAttribute(value, ElementId_, SessionId_)
End Function
' Returns element.innerHTML '2021/6/30 add ishi
Public Function GetInnerHTML() As String
GetInnerHTML = Driver_.GetInnerHTML(ElementId_, SessionId_)
End Function
' Returns element.outerHTML '2021/6/30 add ishi
Public Function GetOuterHTML() As String
GetOuterHTML = Driver_.GetOuterHTML(ElementId_, SessionId_)
End Function
On the WebDriver functions GetInnerHTML and GetOuterHTML, suggest the following changes to default to document body if ElementId not specified:
Public Function GetInnerHTML(Optional ByVal ElementId As String = vbNullString, Optional ByVal SessionId As String = vbNullString) As String
Dim Script As String
If ElementId = vbNullString Then
Script = "return document.body.innerHTML"
Else
Script = "return arguments[0].innerHTML"
End If
GetInnerHTML = ExecuteScript(Script, vbNullString, ElementId, SessionId)
End Function
Public Function GetOuterHTML(Optional ByVal ElementId As String = vbNullString, Optional ByVal SessionId As String = vbNullString) As String
Dim Script As String
If ElementId = vbNullString Then
Script = "return document.body.outerHTML"
Else
Script = "return arguments[0].outerHTML"
End If
GetOuterHTML = ExecuteScript(Script, vbNullString, ElementId, SessionId)
End Function
@ezagdd , if you don't mind me asking, why does GetAttribute handle the cases where user inputs "innerHTML" or "outerHTML"? I'm not an HTML nor JavaScript expert so was wondering... Thx!
Below is a proposal for adding HasAttribute function, and modifying GetAttribute... Any comments?
'added by ezagdd originally, modified by GCUser99
Public Function GetAttribute(ByVal value As String, element As WebElement, Optional ByVal sessionId As String = vbNullString) As String
Dim Script As String
Script = "return arguments[0].getAttribute('" & value & "')"
If HasAttribute(value, element, sessionId) Then
'below will return a VBA Null if attribute does not exist
GetAttribute = ExecuteScript(Script, vbNullString, element, sessionId)
Else
GetAttribute = ""
End If
End Function
'GCUser99 added
Public Function HasAttribute(ByVal value As String, element As WebElement, Optional ByVal sessionId As String = vbNullString) As Boolean
Dim Script As String
Script = "return arguments[0].hasAttribute('" & value & "')"
HasAttribute = ExecuteScript(Script, vbNullString, element, sessionId)
End Function
GetAttributeで「innerHTML」や「outerHTML」を処理しているのは当時のテストの名残かもしれません。ハッキリ言って覚えていないです。 ところで、HasAttribute関数の追加とGetAttributeの修正案ですが、引数が elementID as stringではなく、element as WebElement となっています。#24でコメント頂いた ExecuteScript の引数は elementID as string ですし、他の多くの関数との整合性から elementID as string が良いと思います。
The fact that GetAttribute handles "innerHTML" and "outerHTML" may be a remnant of testing back then. I can't remember for sure. By the way, I added the HasAttribute function and the proposed fix for GetAttribute, but the argument is element as WebElement instead of elementID as string. The argument to ExecuteScript that you commented on in #24 is elementID as string, and I think elementID as string is better for consistency with many other functions.
Public Function GetAttribute(ByVal value As String, _
ByVal ElementId As String, _
Optional ByVal sessionId As String = vbNullString) As String
Dim Script As String
Script = "return arguments[0].getAttribute('" & value & "')"
If HasAttribute(value, ElementId, sessionId) Then
'below will return a VBA Null if attribute does not exist
GetAttribute = ExecuteScript(Script, vbNullString, ElementId, sessionId)
Else
GetAttribute = ""
End If
End Function
Public Function HasAttribute(ByVal value As String, _
ByVal ElementId As String, _
Optional ByVal sessionId As String = vbNullString) As Boolean
Dim Script As String
Script = "return arguments[0].hasAttribute('" & value & "')"
HasAttribute = ExecuteScript(Script, vbNullString, ElementId, sessionId)
End Function
Yeah, I changed my version of the entire code base (@uezo's & your contributions) to take on webelement parameters as opposed to webelement.elementid at the user level. See this discussion... Sorry about the confusion...