none
[vb.net] How to click a javascript button webbrowser

    Question

  • Hi  i'm italian.I i want to click a javascript button on webbrowser, i tri every day but i can't

    code html

    <a class="btn btn-small btn-success" href="#" onclick="javascript:config_page_pns_call_list({id:'31738',monitored_prefix:'39',monitored_number_no_prefix:'3457777777',service_prefix:'39',service_number_no_prefix:'0694802015',status:'VALIDATED',insert_date:'2013-05-10 19:29:48',validate_date:'2013-05-10 19:34:37',active_until:'2112-05-10 19:29:48',active:'1',flat_sms_until:'null',flat_sms_active:'0',send_email:'1',send_sms:'0',send_all_sms:'null',recall:'0',record_call:'0'})"><i class="icon-list icon-white"></i> Elenco chiamate</a>

    Someone can help me?

    Thanks

    Thursday, June 27, 2013 5:52 PM

Answers

  • The solution

     For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("a")
                If Element.OuterHtml.Contains("id:'31738'") Then
                    Element.InvokeMember("click")
                    Exit For
                End If
            Next Element

    ;)

    • Marked as answer by mattiasrk Friday, June 28, 2013 10:51 PM
    Friday, June 28, 2013 4:28 PM

All replies

  • Hello,

    Conceptually speaking whatever you want to run/invoke must have a way to identify the element. The most common method is by name yet we can not always depend on a name. Another idea is the text of a element but the same is true here as the text of the element may change.

    The following invokes a javascript for a button and link by InnerText.

    Public Class KSG_Standard_Form1
        Private Sub KSG_Standard_Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            mainBrowser.DocumentText =
                <html>
                    <head>
                        <title>Demo</title>
                        <script type="text/javascript">function TrafficExchageViewAds() { alert('Your code goes here.'); }</script>
                    </head>
                    <body>
                        <button
                            onclick="TrafficExchageViewAds();"
                            class="buttontetv green"
                            style="height:55px;">VIEW WEBSITES
                        </button>
                        <p>
                            <a href="#" onclick="javascript:alert('Home');">Home</a>
                        </p>
                        <button
                            onclick="javascript:alert('Hello');"
                            class="buttontetv green"
                            style="height:55px;">HELLO
                        </button>
                    </body>
                </html>.ToString
            While mainBrowser.ReadyState <> WebBrowserReadyState.Complete
                Application.DoEvents()
            End While
        End Sub
        Private Sub cmdDemo1_Click(sender As Object, e As EventArgs) Handles cmdDemo1.Click
            Dim myLink =
                (
                    From T In mainBrowser.Document.GetElementsByTagName("button").Cast(Of HtmlElement)()
                    Where T.InnerText = "VIEW WEBSITES"
                ).FirstOrDefault
            If myLink IsNot Nothing Then
                myLink.InvokeMember("Click")
            Else
                MessageBox.Show("Not found")
            End If
        End Sub
        Private Sub cmdDemo2_Click(sender As Object, e As EventArgs) Handles cmdDemo2.Click
            Dim myLink =
                (
                    From T In mainBrowser.Document.GetElementsByTagName("a").Cast(Of HtmlElement)()
                    Where T.InnerText = "Home"
                ).FirstOrDefault
            If myLink IsNot Nothing Then
                myLink.InvokeMember("Click")
            Else
                MessageBox.Show("Not found")
            End If
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    • Proposed as answer by Andrew Morton Friday, June 28, 2013 5:27 PM
    • Unproposed as answer by mattiasrk Friday, June 28, 2013 10:50 PM
    Thursday, June 27, 2013 6:44 PM
  • Thank You!, so this?

    Public Class KSG_Standard_Form1
        Private Sub KSG_Standard_Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            mainBrowser.DocumentText =
               <a class="btn btn-small btn-success" href="#" onclick="javascript:config_page_pns_call_list({id:'31738',monitored_prefix:'39',monitored_number_no_prefix:'3457777777',service_prefix:'39',service_number_no_prefix:'0694802015',status:'VALIDATED',insert_date:'2013-05-10 19:29:48',validate_date:'2013-05-10 19:34:37',active_until:'2112-05-10 19:29:48',active:'1',flat_sms_until:'null',flat_sms_active:'0',send_email:'1',send_sms:'0',send_all_sms:'null',recall:'0',record_call:'0'})"><i class="icon-list icon-white"></i> Elenco chiamate</a>.ToString
            While mainBrowser.ReadyState <> WebBrowserReadyState.Complete
                Application.DoEvents()
            End While
        End Sub
        Private Sub cmdDemo1_Click(sender As Object, e As EventArgs) Handles cmdDemo1.Click
            Dim myLink =
                (
                    From T In mainBrowser.Document.GetElementsByTagName("button").Cast(Of HtmlElement)()
                    Where T.InnerText = " Elenco chiamate"
                ).FirstOrDefault
            If myLink IsNot Nothing Then
                myLink.InvokeMember("Click")
            Else
                MessageBox.Show("Not found")
            End If
        End Sub

    Thursday, June 27, 2013 6:50 PM
  • Looks correct to me, if it does not work try losing the space in front of Elenco chiamate.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Thursday, June 27, 2013 6:55 PM
  • Not correct... msgbox return not found,both with the space  both without
    Thursday, June 27, 2013 7:07 PM
  • I just ran the following and all worked fine

    Public Class KSG_Standard_Form1
        Private Sub KSG_Standard_Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            mainBrowser.DocumentText =
               <a class="btn btn-small btn-success" href="#" onclick="javascript:config_page_pns_call_list({id:'31738',monitored_prefix:'39',monitored_number_no_prefix:'3457777777',service_prefix:'39',service_number_no_prefix:'0694802015',status:'VALIDATED',insert_date:'2013-05-10 19:29:48',validate_date:'2013-05-10 19:34:37',active_until:'2112-05-10 19:29:48',active:'1',flat_sms_until:'null',flat_sms_active:'0',send_email:'1',send_sms:'0',send_all_sms:'null',recall:'0',record_call:'0'})"><i class="icon-list icon-white"></i> Elenco chiamate</a>.ToString
            While mainBrowser.ReadyState <> WebBrowserReadyState.Complete
                Application.DoEvents()
            End While
        End Sub
        Private Sub cmdDemo2_Click(sender As Object, e As EventArgs) Handles cmdDemo2.Click
            Dim Links =
                (
                    From T In mainBrowser.Document.GetElementsByTagName("a").Cast(Of HtmlElement)()
                ).ToList
            ' Find one link
            For Each link In Links
                MessageBox.Show(link.InnerText)
            Next
            Dim myLink =
                (
                    From T In mainBrowser.Document.GetElementsByTagName("a").Cast(Of HtmlElement)()
                    Where T.InnerText = "Elenco chiamate"
                ).FirstOrDefault
            If myLink IsNot Nothing Then
                ' Lands here for me
                myLink.InvokeMember("Click")
            Else
                MessageBox.Show("Not found")
            End If
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Thursday, June 27, 2013 7:25 PM
  • Thanks, but the page there are 2 button called Elenco chiamate.

    Elenco chiamate button 1 code:

    <a class="btn btn-small btn-success" href="#" onclick="javascript:config_page_pns_call_list({id:'31738',monitored_prefix:'39',monitored_number_no_prefix:'3457752452',service_prefix:'39',service_number_no_prefix:'0694802015',status:'VALIDATED',insert_date:'2013-05-10 19:29:48',validate_date:'2013-05-10 19:34:37',active_until:'2112-05-10 19:29:48',active:'1',flat_sms_until:'null',flat_sms_active:'0',send_email:'1',send_sms:'0',send_all_sms:'null',recall:'0',record_call:'0'})"><i class="icon-list icon-white"></i> Elenco chiamate</a>

    Elenco chiamate button 2 code:

    <a class="btn btn-small btn-success" href="#" onclick="javascript:config_page_pns_call_list({id:'39110',monitored_prefix:'39',monitored_number_no_prefix:'3491906664',service_prefix:'39',service_number_no_prefix:'0694802015',status:'VALIDATED',insert_date:'2013-05-26 17:54:23',validate_date:'2013-05-26 17:55:55',active_until:'2112-05-26 17:54:23',active:'1',flat_sms_until:'null',flat_sms_active:'0',send_email:'1',send_sms:'0',send_all_sms:'null',recall:'0',record_call:'0'})"><i class="icon-list icon-white"></i> Elenco chiamate</a>

    I want to consider only the second button.

    So, with your code, the are some issue.. see my video

    http://www.youtube.com/watch?v=frki9rrZNWc

    Thursday, June 27, 2013 8:05 PM
  • Hello,

    It would have been good to have indicated there were two buttons with the exact same text as now I can tell you what you want is not completely 100% possible.

    Here is what you can work with. Beings that you have indicated there are two identical elements this will invoke the second element.

    BTW I am not looking at your video

    Dim Links =
        (
            From T In mainBrowser.Document.GetElementsByTagName("a").Cast(Of HtmlElement)()
            Where T.InnerText = "Elenco chiamate"
        ).ToList
    If Links.Count = 2 Then
        Links(1).InvokeMember("Click")
    End If


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Thursday, June 27, 2013 9:49 PM
  • Hi, your code not working, it did not click anything..

    try now to watch

    Thursday, June 27, 2013 9:56 PM
  • Everything I give you has been tested against the elements you provided and works. If it is not working for you there is something else you have not revealed that is causing the code I provided to fail.

    Again I am not viewing the video.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Thursday, June 27, 2013 11:24 PM
  • Can I send you the project?

    what do you mean for " i am not viewing the video"? you don't want watch video or you can't watch?
    • Edited by mattiasrk Friday, June 28, 2013 11:24 AM ....
    Friday, June 28, 2013 11:21 AM
  • My company has security polices which prohibit viewing videos outside our firewall.

    No you can't send me your project.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Friday, June 28, 2013 11:30 AM
  • 1 button

    2 button

    I want to consider only the second button.


    Friday, June 28, 2013 11:44 AM
  • First off the image is way too small, secondly an image does not help as being an image I can not do anything with it, third even with an image I have to stand by my prior response and comments that if the elements are both the same the only thing different is the index into the document which I already addressed. Nothing new to add.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Friday, June 28, 2013 1:46 PM
  • The solution

     For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("a")
                If Element.OuterHtml.Contains("id:'31738'") Then
                    Element.InvokeMember("click")
                    Exit For
                End If
            Next Element

    ;)

    • Marked as answer by mattiasrk Friday, June 28, 2013 10:51 PM
    Friday, June 28, 2013 4:28 PM
  • That goes back to my first reply

    "Conceptually speaking whatever you want to run/invoke must have a way to identify the element."


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Friday, June 28, 2013 5:21 PM