Visual Studio - getElementsByTagName() -

问题描述 投票:0回答:1

我是Visual Basic的新手,我希望创建一个加载浏览器并将您登录到特定站点的程序。

但是,我尝试登录的网站的用户名和密码字段没有要提取的ID或名称。

有没有办法让GetElementByClass?任何帮助表示赞赏。我似乎无法在网上找到太多文档。

相近:

WebBrowser1.Document.GetElementById("username").SetAttribute("Value",yourUsername)

例如,我想要的输入框是:

<input type="text" class="gwt-TextBox" maxlength="50" style="width: 180px;">

到目前为止,这是我提出的:

Dim theElementCollection As HtmlElementCollection
theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")

For Each curElement As HtmlElement In theElementCollection

Dim controlName As String = curElement.GetAttribute("className").ToString

'MessageBox.Show(curElement.GetAttribute("className").ToString)
' This shows classNames of gwt-TextBox and gwt-PasswordTextBox

Dim user As String = "user"
Dim pass As String = "pass"

WebBrowser1.Document.All.GetElementsByName("gwtTextBox").SetAttribute("value", user)
WebBrowser1.Document.All.GetElementsByName("gwt-PasswordTextBox").SetAttribute("value", user)

我现在看到SetAttribute不是HtmlElement Collection的成员。

html vb.net visual-studio
1个回答
0
投票

完整代码,在@Jimi(https://stackoverflow.com/users/7444103/jimi)的帮助下

   Dim inputElementCollection As HtmlElementCollection
    inputElementCollection = WebBrowser1.Document.GetElementsByTagName("input")

    Dim buttonCollection As HtmlElementCollection
    buttonCollection = WebBrowser1.Document.GetElementsByTagName("button")

    For Each curElement As HtmlElement In inputElementCollection
        Dim inputName As String = curElement.GetAttribute("className").ToString
        'MessageBox.Show(curElement.GetAttribute("className").ToString)
        'gwt-TextBox
        'gwt-PasswordTextBox

        Dim username = uname.Text
        Dim password = passwd.Text

        If curElement.GetAttribute("className") = "gwt-TextBox" Then
            curElement.SetAttribute("value", username)
        End If

        If curElement.GetAttribute("className") = "gwt-PasswordTextBox" Then
            curElement.SetAttribute("value", password)
        End If
    Next

    For Each button As HtmlElement In buttonCollection
        Dim buttonName As String = button.GetAttribute("className").ToString

        If button.GetAttribute("className") = "gwt-Button" Then
            button.InvokeMember("click")
        End If
    Next
© www.soinside.com 2019 - 2024. All rights reserved.