vb 2010 WebBrowser1 如何调用指定内容
手机归属地查询网http://www.showji.com/search.htmtextbox1中输入要查询的手机号单击按钮时候textbox1中的号码发送到网页中并查询...
手机归属地查询网 http://www.showji.com/search.htm
textbox1中输入要查询的手机号
单击按钮时候 textbox1 中的号码发送到网页中并查询归属地
问题是结果不能显示textbox2,需要代码或帮我修改我的代码
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.WebBrowser1.Navigate("http://www.showji.com/search.htm?m=")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Me.TextBox1.Text = "" Then Exit Sub
If Me.TextBox2.Text <> "" Then
Me.TextBox2.Text = ""
End If
Me.WebBrowser1.Document.GetElementById("txtProvince").SetAttribute("innerText", "")
Me.WebBrowser1.Document.GetElementById("m").SetAttribute("value", Me.TextBox1.Text)
Me.WebBrowser1.Document.GetElementById("btnQuery").InvokeMember("click")
Do
Application.DoEvents()
Loop Until WebBrowser1.Document.GetElementById("txtProvince").GetAttribute("innerText") <> ""
TextBox2.Text = WebBrowser1.Document.GetElementById("txtProvince").GetAttribute("innerText")
End Sub
End Class 展开
textbox1中输入要查询的手机号
单击按钮时候 textbox1 中的号码发送到网页中并查询归属地
问题是结果不能显示textbox2,需要代码或帮我修改我的代码
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.WebBrowser1.Navigate("http://www.showji.com/search.htm?m=")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Me.TextBox1.Text = "" Then Exit Sub
If Me.TextBox2.Text <> "" Then
Me.TextBox2.Text = ""
End If
Me.WebBrowser1.Document.GetElementById("txtProvince").SetAttribute("innerText", "")
Me.WebBrowser1.Document.GetElementById("m").SetAttribute("value", Me.TextBox1.Text)
Me.WebBrowser1.Document.GetElementById("btnQuery").InvokeMember("click")
Do
Application.DoEvents()
Loop Until WebBrowser1.Document.GetElementById("txtProvince").GetAttribute("innerText") <> ""
TextBox2.Text = WebBrowser1.Document.GetElementById("txtProvince").GetAttribute("innerText")
End Sub
End Class 展开
1个回答
展开全部
原因就是网页的一些意外错误了,在调试的卖弯时候发现并不是每次都是空的。直接把这个东西放到try语句块或者做非空判断就可以了。在你代码的基础上略微改了点。
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Me.TextBox1.Text = "" Then Exit Sub
If Me.TextBox2.Text <> "" Then
Me.TextBox2.Text = ""
End If
Dim isloop As Boolean = True
Me.WebBrowser1.Document.GetElementById("txtProvince").SetAttribute("innerText", "")
Me.WebBrowser1.Document.GetElementById("m").SetAttribute("value", Me.TextBox1.Text)
Me.WebBrowser1.Document.GetElementById("btnQuery").InvokeMember("click")
Do
Application.DoEvents()
Try
If WebBrowser1.Document.GetElementById("txtProvince").GetAttribute("innerText") <> "" Then
isloop = False
End If
Catch ex As Exception
End Try
Loop Until isloop = False
TextBox2.Text = WebBrowser1.Document.GetElementById("txtProvince").GetAttribute("innerText")
End Sub
甚至于可以直接这么改,这时候只需要更改一句,而且执行效率也比上面的代码高的多
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Me.TextBox1.Text = "" Then Exit Sub
If Me.TextBox2.Text <> "" Then
Me.TextBox2.Text = ""
End If
念戚Me.WebBrowser1.Document.GetElementById("txtProvince").SetAttribute("innerText", "")
Me.WebBrowser1.Document.GetElementById("m").SetAttribute("value", Me.TextBox1.Text)
Me.WebBrowser1.Document.GetElementById("btnQuery").InvokeMember("click")
Do
Application.DoEvents()
中高闷 Loop Until IsNothing(WebBrowser1.Document.GetElementById("txtProvince")) = False AndAlso WebBrowser1.Document.GetElementById("txtProvince").GetAttribute("innerText") <> ""
TextBox2.Text = WebBrowser1.Document.GetElementById("txtProvince").GetAttribute("innerText")
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询