Webbrowser code help

Primeval

Newbie
Joined
Dec 9, 2012
Messages
23
Reaction score
0
Yoo,

Is there any way to get this working?
Code:
WebBrowser1.Navigate
        While Not WebBrowser1.ReadyState = WebBrowserReadyState.Complete
            Application.DoEvents()
        End While
        WebBrowser1.Document.GetElementById("drug_2").SetAttribute("value", 400)
        WebBrowser1.Document.GetElementById("sell").InvokeMember("click")
WebBrowser1.Document.GetElementById("drug_6").SetAttribute("value", 400)
            WebBrowser1.Document.GetElementById("buy").InvokeMember("click")
It skips the last 2 lines.. Is there any problem with the code?


BUMP, Any solution for this code? Really need it..
 
The proper way would be to handle the DocumentCompleted event instead of looping for the proper state
 
Rather then waiting for the document complete, its easier if you search for a key text that will be present from page to page, the document completed is an unreliable func.

For example, while WebBrowser1.Document.GetElementById("drug_2") is null wait until its not.
 
After
Code:
[COLOR=#FFFFCC]WebBrowser1.Document.GetElementById("sell").InvokeMember("click")[/COLOR]
a new webpage will load or? Then you need to wait till the document is complete.
If the webpage is loaded you can set the value.
Code:
[COLOR=#FFFFCC]WebBrowser1.Document.GetElementById("drug_6").SetAttribute("value", 400)
[/COLOR][COLOR=#FFFFCC]            WebBrowser1.Document.GetElementById("buy").InvokeMember("click")[/COLOR]


This should work for you, if not please PM me :).
Code:
 Public Sub WaitForPageLoad()
        AddHandler [COLOR=#FFFFCC]WebBrowser1[/COLOR].DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
        While Not pageready
            Application.DoEvents()
        End While
        pageready = False
    End Sub
    Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
        If [COLOR=#FFFFCC]WebBrowser1[/COLOR].ReadyState = WebBrowserReadyState.Complete Then
            pageready = True
            RemoveHandler [COLOR=#FFFFCC]WebBrowser1[/COLOR].DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
        End If
    End Sub

WebBrowser1.Document.GetElementById("drug_2").SetAttribute("value", 400)
WebBrowser1.Document.GetElementById("sell").InvokeMember("click")
WaitForPageLoad()
WebBrowser1.Document.GetElementById("drug_6").SetAttribute("value", 400)
WebBrowser1.Document.GetElementById("buy").InvokeMember("click")
WaitForPageLoad()
 
Last edited:
WebBrowser1.Document.GetElementById("sell").InvokeMember("click") -- > Problem is that "click" is not synchronous call.

If "drug_6" only appears after you click the "sell" button, then its possible the script is calling faster than the page can refresh itself.

WebBrowser1.Document.GetElementById("drug_6").SetAttribute("value", 400) --> drug_6 doesn't exist so nothing happens WebBrowser1.Document.GetElementById("buy").InvokeMember("click")

So you need to wait for "drug_6" != null (if its ajax, the page will still be complete, so waiting for READY_STATE won't work)
 
Back
Top