简体   繁体   中英

vb.net click a javascript button

I have a vb.net Windows form. In the form I am running a web browser control. I am trying to click on a button using vb code. I found this example of invoking a js function directly but I am not having any luck. How can I click a js button on VB

The html for the button is-

<input class="boldbutton" type="button" value="Verify"
   onclick="this.form.knob.value='ReqVerify';this.form.verify.value=1;this.form.submit()">

My vb code is-

WebBrowser1.Document.InvokeScript(
    "this.form.knob.value='ReqVerify';this.form.verify.value=1;this.form.submit()"
)

I am not sure where I am going wrong. After the submit button is pressed there is a log that displays on the web page showing the activity. I am not seeing any activity when running my script, only if I manually push the button from another browser (not the one in the windows form).

Your issue is that your vb.net's javascript call has no reference point for this . When you click the button in the web browser, this refers to the button.

When you call WebBrowser1.Document.InvokeScript this refers to something global, perhaps the window?

Either way, you can just invoke the button click. Do this by adding an id, for example btnSubmit to the button. Then do:

Dim search As HtmlElement = webBrowser1.Document.GetElementById("btnSubmit")     
If search IsNot Nothing Then        
      For Each ele as HtmlElement in search.Parent.Children
          If ele.TagName.ToLower() = "input" AndAlso ele.Id.ToLower() = "btnSubmit" Then                
              ele.InvokeMember("click")
              Exit For
          End If
      Next
  End If

Taken from (c#): https://stackoverflow.com/a/5227644/1160796

Another option is to fix your javascript code. Do that by changing this.form to the form's id. Again, if there isn't an id then set one, ex frmMain . Then do:

WebBrowser1.Document.InvokeScript("frmMain.knob.value='ReqVerify';frmMain.verify.value=1;frmMain.submit()")

If you don't have the ability to change the website source then use document.forms . Assuming it's the only form:

WebBrowser1.Document.InvokeScript("document.forms(0).knob.value='ReqVerify';document.forms(0).verify.value=1;document.forms(0).submit()")

Imports System Imports System.Data Imports System.Data.OleDb

Public Class Form1 Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\VulnScanData.accdb;Persist Security Info=True;Jet OLEDB:Database Password=************")

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'opens connection to database
    con.Open()
    WebBrowser1.Navigate("https://fs-enterprise.my.private.url/")
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles BtnLogin.Click
    WebBrowser1.Document.GetElementById("name").SetAttribute("value", TextBox1.Text)
    WebBrowser1.Document.GetElementById("password").SetAttribute("value", TextBox2.Text)
    WebBrowser1.Document.GetElementById("Logon").InvokeMember("click")
End Sub

Private Sub BtnQuery_Click(sender As Object, e As EventArgs) Handles BtnQuery.Click
    'open transaction object
    Dim trans As OleDb.OleDbTransaction
    trans = con.BeginTransaction
    'define the command which allows you to read, write or update the db
    Dim cmd As New OleDb.OleDbCommand
    'define the query        
    cmd.CommandText = "SELECT [Ticket ID] AS Ticket_ID FROM [Table_Main] WHERE ([Ticket Days OverDue] >= 1)"
    'assign the connection
    cmd.Connection = con
    'assign the transaction
    cmd.Transaction = trans
    'execte the command
    Dim myreader As OleDb.OleDbDataReader
    myreader = cmd.ExecuteReader
    Do While myreader.Read
        MsgBox(myreader.Item("Ticket_ID"))
        Dim result As String = myreader.Item("Ticket_ID")
        WebBrowser1.Navigate("https://fs-enterprise.my.private.url/remediation/ticket.exp?ticket=" & result)
        Do While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
            Application.DoEvents()
        Loop
        '<input class="boldbutton" type="button" value="Verify" onclick="this.form.knob.value='ReqVerify';this.form.verify.value=1;this.form.submit()"></td>
        'WebBrowser1.Document.InvokeScript("document.forms(0).knob.value='ReqVerify';document.forms(0).verify.value=1;document.forms(0).submit()")
        WebBrowser1.Document.InvokeScript("this.form.knob.value='ReqVerify';this.form.verify.value=1;this.form.submit()")
        MessageBox.Show("Submitting Ticket")
    Loop
    myreader.Close()
    con.Close()
End Sub

End Class

I was able to get this to work. Here is my code

For Each el As HtmlElement In WebBrowser1.Document.GetElementsByTagName("INPUT")
            If (el.GetAttribute("value").Equals("Verify")) Then
                el.InvokeMember("click")
            End If
        Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM