我在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
答案 0 :(得分:2)
你在这里遇到了几个问题。让我们从文本框输入开始。如果您查看该网站的登录页面的html源代码,用户名和密码的输入没有ID属性,它们只使用Name。此外,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