Results 1 to 16 of 16

Thread: VB 2010 Webbrowser - How to click a link TWICE

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Posts
    19

    Exclamation VB 2010 Webbrowser - How to click a link TWICE

    Hello All;

    I'm new to VB and hoping someone could help me with the following code. I have searched this forum and several others but couldn't find the correct answer .
    Thank you in advance for your help.

    What I'm trying to do is sort the DATE in descending order in a table by clicking the column heading link TWICE on a webpage. The link is a javascript. I'm successful in clicking it once, but I need to click it one more time (or may be I'm doing it all wrong in the first place) to get it to sort the way I want. I tried adding "elment. InvokeMember("click")" twice but that didn't work.

    Here's HTML part:

    HTML Code:
    <th scope="col" style="white-space:nowrap;"><a href="javascript:PTWCControl_3_1.doPostBack('5709','frmGeneralInfo_5709','GridView1_5709','Sort$LastActivityDate')">Last Activity Date</a></th>
    Here's the VB code. This code is working perfectly, by clicking once on the link, but I need to click it TWICE:

    Code:
    For Each elm As HtmlElement In WebBrowser1.Document.Links
               If elm.GetAttribute("href") = "javascript:__doPostBack('ctl08$btnLogOut','')" Then
                            elm.InvokeMember("click")
               End If
    Next
    Thank you kindly. Jb

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Posts
    19

    Re: VB 2010 Webbrowser - How to click a link TWICE

    Hello All;

    sorry I listed the wrong VB code here's correct one:

    Code:
    For Each elm As HtmlElement In WebBrowser1.Document.Links
                    If elm.GetAttribute("href") = "javascript:PTWCControl_3_1.doPostBack('5709','frmGeneralInfo_5709','GridView1_5709','Sort$LastActivityDate')" Then
    
                        elm.InvokeMember("click")
    
                    End If
     Next
    Thanks, jb

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: VB 2010 Webbrowser - How to click a link TWICE



    elm.InvokeMember("click")
    elm.InvokeMember("click")


    ... Shirley?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Posts
    19

    Question Re: VB 2010 Webbrowser - How to click a link TWICE

    Quote Originally Posted by dunfiddlin View Post


    elm.InvokeMember("click")
    elm.InvokeMember("click")


    ... Shirley?
    Hi dunfiddlin ;

    I tried that but didn't work. For some reason it ignore both "click" events or just click only once. Any thoughts? I don't know if this makes any difference, but I'm calling this from inside WebBrowser1_DocumentCompleted:

    Code:
    Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    
            For Each elm As HtmlElement In WebBrowser1.Document.Links
                    If elm.GetAttribute("href") = "javascript:PTWCControl_3_1.doPostBack('5709','frmGeneralInfo_5709','GridView1_5709','Sort$LastActivityDate')" Then
    
                        elm.InvokeMember("click")
                        elm.InvokeMember("click")
    
                    End If
            Next
    
    End sub
    Thanks

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: VB 2010 Webbrowser - How to click a link TWICE

    It may need a delay between the two clicks to ensure that they're registered separately. There are various ways to do that. I would probably try a Sleep ( Threading.Thread.Sleep(100) first. You can fiddle about with the period. Tenth of a second is just a reasonable place to start.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Posts
    19

    Re: VB 2010 Webbrowser - How to click a link TWICE

    Thanks! I will try that.

  7. #7
    Hyperactive Member
    Join Date
    Jun 2013
    Posts
    312

    Re: VB 2010 Webbrowser - How to click a link TWICE

    To my understanding, threading.thread.sleep is not going to help and this also gives the illusion of a crashed application (depending on the sleep-time set of course.) Its also not practical to set a webbrowser handler navigated or document complete to sleep during that process in my opinion. (This will show the stuck 'loading' icon on the browser. Which is not good.) When the page loads, refresh the control by right clicking. If it invokes after a refresh, let me know?

    Have you also done this? http://www.vbforums.com/showthread.p...=1#post4560105

  8. #8
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,390

    Re: VB 2010 Webbrowser - How to click a link TWICE

    Anything a browser does is performed on a secondary thread. So no the page wont be effected.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Posts
    19

    Re: VB 2010 Webbrowser - How to click a link TWICE

    Hello, dunfiddlin; Mucker;

    threading.thread.sleep didn't work. So I removed it and used two Invokes. It do sort the Date column on the first Invoke to ascending. Doesn't do the second (for descending). But when I refresh the page it went back to a unsorted list (this is the default state of the page).

    Thanks jb

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Posts
    19

    Re: VB 2010 Webbrowser - How to click a link TWICE

    Quote Originally Posted by Mucker View Post
    This page is one of many pages I need to navigate after the login. But not as part of login process. So I'm not sure if this will help or I could be wrong???

  11. #11
    Hyperactive Member
    Join Date
    Jun 2013
    Posts
    312

    Re: VB 2010 Webbrowser - How to click a link TWICE

    Quote Originally Posted by ident View Post
    Anything a browser does is performed on a secondary thread. So no the page wont be effected.
    Perhaps you can explain the logic behind what you are saying Ident? (Not trying to be argumentative, just looking to better understand if I may be wrong.) However, if a thread with a handler Navigating is Navigating and whatever it does while doing so is done only when that thread is active. So if you set that thread to sleep for 5 minutes, how is the WebBrowser Document Complete handler going to initiate when you told the browser navigating to completion to sleep?


    Jay, If i was you mate, I would honestly not use VB to manipulate all these little buttons and link clicks. I personally advise if you need a said page to have a descending/ascending order, I'd set it on that page in html/php code rather than invoking it through VB. With JavaScript errors suppressed, you will face problems using javascript in your browser anyway.

    The below is not a practical solution, and you should dig to find out why it wont work. Add a form to your html page and try to invoke the submit button to direct you to google. If that works, you know its a javascript thing.

    Code:
    Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated 'This allows the page to be worked on as most of the content is accessible as its navigated to the url already
            For Each elm As HtmlElement In WebBrowser1.Document.Links
                    If elm.GetAttribute("href") = "javascript:PTWCControl_3_1.doPostBack('5709','frmGeneralInfo_5709','GridView1_5709','Sort$LastActivityDate')" Then
    
                        elm.InvokeMember("click")
    ClickAgain()
                    End If
            Next
    
    End sub
    Sub ClickAgain (sender As Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    
    Threading.Thread.Sleep(300) ' Play with the time to see how it acts - It will create a delay between clicks
    If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then 'Ensure that everything loaded
    For Each elm As HtmlElement In WebBrowser1.Document.Links
                    If elm.GetAttribute("href") = "javascript:PTWCControl_3_1.doPostBack('5709','frmGeneralInfo_5709','GridView1_5709','Sort$LastActivityDate')" Then
                        elm.InvokeMember("click")
                    End If
            Next
    End Sub
    I think you have something else in your WebBrowser handlers that is preventing the second invoke, and you're not posting it or you have two WebBrowsers on your form... Invoking twice is never needed unless you have authentication or cookie issues with the Control and commonly pages not being cached properly.
    Last edited by Mucker; Jan 22nd, 2014 at 02:51 PM. Reason: typo

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Posts
    19

    Re: VB 2010 Webbrowser - How to click a link TWICE

    Mucker ; tried your suggestion with the Sub Clickagain(), but didn't work. You may be right about the javascripts. I'm working with my company website so changing anything with the website is out of my hand. I started this project to make my life a bit easier and learn VB on the way. I may have picked the wrong website to do so.

    anyway...

    Quote Originally Posted by Mucker View Post
    I think you have something else in your WebBrowser handlers that is preventing the second invoke, and you're not posting it or you have two WebBrowsers on your form...
    No, I only have one browser control..

    Quote Originally Posted by Mucker View Post
    Invoking twice is never needed unless you have authentication or cookie issues with the Control and commonly pages not being cached properly.
    .... this got me thinking... When I observe the url, depending on how I navigates to this page, I see two url, like so....
    this when I navigate from the Home page:
    Code:
    https://my.xxxxxx.com/portal/server.pt?open=514&objID=1229034&mode=2
    this when I first, login and then go straight to the page (without stopping on Home page):
    Code:
    https://my.xxxx.com/portal/server.pt?open=514&objID=1229034&parentname=Gateway&parentid=None&mode=2&in_hi_userid=521527&cached=true
    I don't have clue about the importance of these url's to my problem; but do notice the '''&cached=true''' at the end of the 2nd url

    So my be its a cached issue???

  13. #13
    Hyperactive Member
    Join Date
    Jun 2013
    Posts
    312

    Re: VB 2010 Webbrowser - How to click a link TWICE

    Pointless, but adding 2 msgboxs to where your if statement is will state that your javascript links are not dynamically changing when clicked. Yes Javascript can do that. But if it was the case, then your application would throw an exception to the fact that it has nothing to invoke as the element you are referring to is not there. At least it should. Try it but if you get two true box's you wont be fixing your problem without access to the files on the web server. That's the trouble with using a WBC with no FTP access. Must confess to a typo in the post above, but i am sure you seen it otherwise it would not debug Also, If you pm me the website, I can look at the javascript file for you.

    Code:
    Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated 'This allows the page to be worked on as most of the content is accessible as its navigated to the url already
            For Each elm As HtmlElement In WebBrowser1.Document.Links
                    If elm.GetAttribute("href") = "javascript:PTWCControl_3_1.doPostBack('5709','frmGeneralInfo_5709','GridView1_5709','Sort$LastActivityDate')" Then
    MSgBox("True Once")
                        elm.InvokeMember("click")
    
                    End If
            Next
    
    End sub
    Sub WebBrowser1_DocumentCompleted(sender As Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    
    Threading.Thread.Sleep(300) ' Play with the time to see how it acts - It will create a delay between clicks
    If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then 'Ensure that everything loaded
    For Each elm As HtmlElement In WebBrowser1.Document.Links
                    If elm.GetAttribute("href") = "javascript:PTWCControl_3_1.doPostBack('5709','frmGeneralInfo_5709','GridView1_5709','Sort$LastActivityDate')" Then
    MsgBox("True Twice")
                        elm.InvokeMember("click")
                    End If
            Next
    End Sub

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Posts
    19

    Re: VB 2010 Webbrowser - How to click a link TWICE

    Quote Originally Posted by Mucker View Post
    Pointless, but adding 2 msgboxs to where your if statement is will state that your javascript links are not dynamically changing when clicked. Yes Javascript can do that. But if it was the case, then your application would throw an exception to the fact that it has nothing to invoke as the element you are referring to is not there. At least it should. Try it but if you get two true box's you wont be fixing your problem without access to the files on the web server. That's the trouble with using a WBC with no FTP access. Must confess to a typo in the post above, but i am sure you seen it otherwise it would not debug Also, If you pm me the website, I can look at the javascript file for you.
    I tired that.. only gave me one box, which is "True Twice" .. seems like it ignore the Handles WebBrowser1.Navigated ..

    I gave up taking this route and decided to use a button to do the second click. And it worked! although this not ideal. In any case my next task would be to grab the data from the page. So I will use this button to do the clicking and grabbing data etc.

    Thank you for all your help.

  15. #15
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,252

    Re: VB 2010 Webbrowser - How to click a link TWICE

    Quote Originally Posted by jayb View Post
    I gave up taking this route and decided to use a button to do the second click. And it worked!
    Have you tried running that additional code from a timer? I've run into similar problem trying to launch multiple web pages or click multiple buttons one after the other and got to a point where no matter what I did (adding delays, etc) did nothing until I allowed the browser_doc_ completed event to completely finish and exit the sub on its own! My solution was to add a vb timer and start it from the doc_completed event, use variables to decide what to do next in the timers tick event, etc,etc, its a pain but it solved it for me in a couple apps I now have running 24/7 fully automated.

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Jan 2014
    Posts
    19

    Re: VB 2010 Webbrowser - How to click a link TWICE

    Quote Originally Posted by Edgemeal View Post
    My solution was to add a vb timer and start it from the doc_completed event, use variables to decide what to do next in the timers tick event, etc,etc, its a pain but it solved it for me in a couple apps I now have running 24/7 fully automated.
    @Edgemeal Thanks! I should try it. But for now I'm moving on with the Button_Click, and I'll come back to it late.

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