Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Method. GetAttribute, GetInnerHTML, GetOuterHTML #34

Open
ghost opened this issue Oct 23, 2021 · 5 comments
Open

Add Method. GetAttribute, GetInnerHTML, GetOuterHTML #34

ghost opened this issue Oct 23, 2021 · 5 comments

Comments

@ghost
Copy link

ghost commented Oct 23, 2021

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
@GCuser99
Copy link

GCuser99 commented Dec 29, 2021

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

@GCuser99
Copy link

GCuser99 commented Jan 7, 2022

@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!

@GCuser99
Copy link

GCuser99 commented Jan 8, 2022

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

@ghost
Copy link
Author

ghost commented Jan 10, 2022

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

@GCuser99
Copy link

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants