嘿!的
简单的问题,有什么方法可以让webbrowser等到元素存在?我不是一个了不起的编码员,所以这是我尝试做的最好的例子 -
Do While PageLoaded = False
Dim OObject As Object
OObject = WebBrowser1.Document.GetElementById("Element-That-I-Need-Loaded")
If (OObject Is Nothing) Then
Pageloaded = False
Else
PageLoaded = True
End If
Loop
问题在于元素没有ID,所以当我尝试点击它时,我必须做这样的事情 -
For Each altelm As HtmlElement In WebBrowser1.Document.GetElementsByTagName("SPAN")
If altelm.GetAttribute("classname").ToString = "Classname-of-Element-I-Need-Loaded" Then
altelm.Focus()
altelm.InvokeMember("click")
End If
如果措辞不当,我道歉,我是新手:)
谢谢!的
答案 0 :(得分:0)
使用定期检查元素的计时器。在DocumentCompleted
事件中启动计时器,然后在找到元素后停止它。
Private WithEvents WaitTimer As New Timer With {.Interval = 250}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
WebBrowser1.Navigate("http://www.somewebsite.com/")
AddHandler WebBrowser1.DocumentCompleted, AddressOf SomeWebsite_DocumentCompleted
End Sub
Private Sub SomeWebsite_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
WaitTimer.Start()
RemoveHandler WebBrowser1.DocumentCompleted, AddressOf SomeWebsite_DocumentCompleted
End Sub
Private Sub WaitTimer_Tick(sender As Object, e As EventArgs) Handles WaitTimer.Tick
'Look for the element every time the timer ticks.
For Each altelm As HtmlElement In WebBrowser1.Document.GetElementsByTagName("SPAN")
If altelm.GetAttribute("className") = "Classname-of-Element-I-Need-Loaded" Then 'No need to call ToString() since GetAttribute() already returns a string.
altelm.Focus()
altelm.InvokeMember("click")
WaitTimer.Stop() 'Element found, stop the timer.
End If
Next
End Sub