Results 1 to 10 of 10

Thread: Webbrowser Invoke problems

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    South Africa
    Posts
    141

    Webbrowser Invoke problems

    Hello All

    I am trying to save a html table from a website to a .csv file.

    However, I have issues invoking an element. I have managed to login into the site, but cannot work out how to invoke a element (it is just text - no button).

    My VB code is (I have asterixed the username and password - but I can assure you its working) :

    Code:
    Public Class Form1
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            WebBrowser1.Navigate("https://csonline.eskom.co.za")
        End Sub
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            WebBrowser1.Document.GetElementById("accountNumber").SetAttribute("value", "********")
            WebBrowser1.Document.GetElementById("accountPassword").SetAttribute("value", "*******")
    
            WebBrowser1.Document.Forms(0).InvokeMember("submit")
    
    
    
            WebBrowser1.Document.Forms(0).InvokeMember("f10_linkThreeButton")
    
        End Sub
    End Class
    I cannot invoke the text (ID is f10_linkThreeButton) with WebBrowser1.Document.Forms(0).InvokeMember("f10_linkThreeButton").

    The inspect element from the website is :

    Code:
    <a href="javascript:void(0);" style="" id="f10_linkThreeButton" class="GTHREFButton   linkThreeButton quickLinks GTTextButtonH GTVI  GTTextFieldAC " onfocus="gainFocus('f10_linkThreeButton');" onblur="looseFocus();" onclick="clientAPI.postEvent('f10_linkThreeButton',4);return false;" tabindex="-1" title="View your consumption history.">View Consumption History</a>

    Any help would be much appreciated.

    Regards

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Webbrowser Invoke problems

    Surely you have to wait for the login to be processed before invoking that second link.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    South Africa
    Posts
    141

    Re: Webbrowser Invoke problems

    Thank you for replying jmcilhinney.

    I can login with Button3_Click. In the webbrowser window the login is successful, but when I try to invoke the second link, nothing happens ( no error messages).


    If I click on the link in the webbrowser window in my application I get an error : On error has occurred in the script on this page 'CLientAPI' is indefined.

  4. #4
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: Webbrowser Invoke problems

    Quote Originally Posted by GideonE View Post
    Thank you for replying jmcilhinney.

    I can login with Button3_Click. In the webbrowser window the login is successful, but when I try to invoke the second link, nothing happens ( no error messages).


    If I click on the link in the webbrowser window in my application I get an error : On error has occurred in the script on this page 'CLientAPI' is indefined.
    Trying to automate websites is often a bad idea and should only be a last resort. Quite often websites are designed to deliberately prevent this kind of automation (captchas etc.), even when they aren't deliberately stopping automation working, they can be really problematic to automate; often they will break at some point later as the website designer changes things anyway. Additionally this may even be against the T&Cs of the site in question as well.

    Often it is better to see if an API is provided instead of trying to automate things.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Webbrowser Invoke problems

    If you were doing this as a normal user in a web browser, would you have to wait after clicking submit before the second click?

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    South Africa
    Posts
    141

    Re: Webbrowser Invoke problems

    Yes I would.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Webbrowser Invoke problems

    So, given that there's basically zero delay between those two actions in your code, can you see the problem? There is obviously no such link in the current page because the page containing that link hasn't loaded yet, because you haven't waited for it to load.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    South Africa
    Posts
    141

    Re: Webbrowser Invoke problems

    Adding System.Threading.Thread.Sleep(5000) before the second link made no difference.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Webbrowser Invoke problems

    That's not the solution. The solution is wait until the next page has loaded. That's why the WebBrowser has a DocumentLoaded (or the like) event.

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Webbrowser Invoke problems

    The event is DocumentCompleted. You need to test for ReadyState before attempting to click the button. The values are...

    Complete 4
    The control has finished loading the new document and all its contents.

    Interactive 3
    The control has loaded enough of the document to allow limited user interaction, such as clicking hyperlinks that have been displayed.

    Loaded 2
    The control has loaded and initialized the new document, but has not yet received all the document data.

    Loading 1
    The control is loading a new document.

    Uninitialized 0
    No document is currently loaded.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width