Webbrowser code help

Discussion in 'Visual Basic .NET' started by Primeval, Feb 16, 2013.

  1. Primeval

    Primeval Newbie

    Joined:
    Dec 9, 2012
    Messages:
    23
    Likes Received:
    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?
    [hr]
    BUMP, Any solution for this code? Really need it..
     
  2. xuanhung123

    xuanhung123 Newbie

    Joined:
    Nov 17, 2010
    Messages:
    25
    Likes Received:
    2
    Occupation:
    Programmer
    The proper way would be to handle the DocumentCompleted event instead of looping for the proper state
     
  3. cgimaster

    cgimaster Power Member

    Joined:
    Jun 30, 2012
    Messages:
    526
    Likes Received:
    311
    Gender:
    Male
    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.
     
  4. Primeval

    Primeval Newbie

    Joined:
    Dec 9, 2012
    Messages:
    23
    Likes Received:
    0
    Anything else?
     
  5. g4m3r

    g4m3r Junior Member

    Joined:
    Jan 8, 2012
    Messages:
    100
    Likes Received:
    81
    Occupation:
    computer scientist
    Location:
    gsoftwarelab.com
    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: Feb 23, 2013
  6. terebi

    terebi Jr. VIP Jr. VIP Premium Member

    Joined:
    Apr 11, 2011
    Messages:
    357
    Likes Received:
    94
    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)