【问题标题】:How to automatically input in the WebBrowser control如何在 WebBrowser 控件中自动输入
【发布时间】:2017-12-06 15:53:25
【问题描述】:

我在 vb.net 方面非常业余。当我在文本框中输入文本时,它应该能够在webbrowsercontrol上自动输入以及如何单击按钮登录,其中没有getelementbyid。

我也设法使第一部分正确,但是当我从浏览器内部单击登录按钮时,似乎有一个小错误。很久以前做过一个这样的项目,现在找不到源代码了,所以我又从头开始了。

网址:https://app.coins.ph/welcome/login

到目前为止,这是我的代码:

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    WebBrowser1.Document.GetElementById("username").InnerText = TextBox1.Text
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("class") 'Depending on how the source code is formatted on the tag, you may also try Element.OuterHTML, Element.InnerText and Element.OuterText in the line below
        If Element.OuterText.Contains("SIGN IN") Then
            Element.InvokeMember("click")
            Exit For
        End If
    Next Element
End Sub

Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
    WebBrowser1.Document.GetElementById("password").InnerText = TextBox2.Text
End Sub

【问题讨论】:

  • 花几分钟学习how to ask.
  • 呃,提问时,请多花几秒钟时间添加一个有用的标题。这种乞讨可能适用于网站上的所有 1500 万个问题,并且(a)您的问题并不比其他问题更重要,(b)您的问题正在为志愿者创建编辑工作,(c)您实际上是在要求反对会损害您获得好答案的机会,(d) 以上所有这些都是显而易见的。
  • “似乎有一个小错误” - 什么错误?我不知道.net,但看起来你的问题可以做更多的细节。请注意,人们不会访问您的网站来查找此信息 - 您需要为他们提供这些信息。

标签: vb.net


【解决方案1】:

这里有几个问题。让我们从文本框输入开始。如果您查看该网站登录页面的 html 源代码,用户名和密码的输入没有 ID 属性,它们仅使用名称。此外,GetElementsByTagName 正在搜索“用户名”的 html 元素,而不是应有的“输入”。鉴于这两个问题,您应该使用Document.All("[elementName]") 来访问这些输入。至于登录部分,如前所述,GetElementsByTagName 正在寻找 html 元素,因此搜索值“class”不会返回您想要的任何内容。相反,您应该寻找 OuterText 包含“SIGN IN”的“按钮”。应用所有这些更改后,代码变为:

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        WebBrowser1.Navigate("https://app.coins.ph/welcome/login")
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        WebBrowser1.Document.All("username").InnerText = TextBox1.Text
    End Sub

    Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
        WebBrowser1.Document.All("password").InnerText = TextBox2.Text
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("button")
            If Element.OuterText.Contains("SIGN IN") Then
                Element.InvokeMember("click")
                Exit For
            End If
        Next Element
    End Sub

End Class

运行此代码后还有另一个问题

如果您运行上面的示例,您将看到表单域已正确填写并且成功单击了登录按钮,但是会出现一个错误,表明表单域仍然是空白的。即使您也使用WebBrowser1.Document.All("username").SetAttribute("value", TextBox1.Text) 设置输入值,也会发生相同的错误。这可能是因为该网站的开发人员正在使用某种 javascript 来检测按键,出于某种原因……不可能知道为什么,但事实就是如此。所以你只剩下自己实际模拟按键了。如果您这样做,该网站将使用用户名和密码成功登录。你有两种方法可以做到这一点。更简洁的方法是一次发送所有密钥并像这样登录:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    WebBrowser1.Focus()
    WebBrowser1.Document.All("username").Focus()
    For Each c As Char In TextBox1.Text.ToCharArray
        SendKeys.SendWait(c)
    Next
    WebBrowser1.Document.All("password").Focus()
    For Each c As Char In TextBox2.Text.ToCharArray
        SendKeys.SendWait(c)
    Next
    For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("button")
        If Element.OuterText.Contains("SIGN IN") Then
            Element.InvokeMember("click")
            Exit For
        End If
    Next Element
End Sub

但是,如果您仍然希望每个字符在您键入时出现,为了反映您当前使用的 TextChanged 事件逻辑的功能,您必须使用 KeyPress 事件并基本上像这样转发击键:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("button")
        If Element.OuterText.Contains("SIGN IN") Then
            Element.InvokeMember("click")
            Exit For
        End If
    Next Element
End Sub

Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    WebBrowser1.Focus()
    WebBrowser1.Document.All("username").Focus()
    SendKeys.SendWait(e.KeyChar)
    TextBox1.Focus()
End Sub

Private Sub TextBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox2.KeyPress
    WebBrowser1.Focus()
    WebBrowser1.Document.All("password").Focus()
    SendKeys.SendWait(e.KeyChar)
    TextBox2.Focus()
End Sub

【讨论】:

  • 哇,这似乎解决了我的问题,即我在反复试验中被困了很长时间。非常感谢,这真的很有帮助。
  • 我遇到了另一个问题。无论如何我可以通过facebook直接联系你吗?这次真的很需要你的帮助,以后也需要一些参考,谢谢
  • 把你的问题发到这个网站上吧,StackOverflow 上有很多比我优秀的开发者
  • 这是我现在遇到的问题:snag.gy/Eusdab.jpg(pic) 这也是我当前的代码:pastebin.com/r6n2ga0X 抱歉,如果我在此评论上提出新问题,我对 stackoverflow 还是很陌生,只是在尝试在这里回答,而且我似乎不能再提出新问题了
  • @KennOmisol,在 SO 上说“谢谢”的最佳方式是接受并点赞您认为有帮助的答案。
猜你喜欢
  • 2011-07-26
  • 1970-01-01
  • 2011-12-21
  • 2019-09-30
  • 2010-12-04
  • 2013-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多