0

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).

2
  • Why do you want to call web browser control from a Windows Form application? Both are different platforms. Commented May 12, 2015 at 16:19
  • My form performs a query on an access database. It pulls results and stores them as a variable. It uses the variable to open the correct web page. Once the web page is opened it should click on the verify button. Commented May 12, 2015 at 16:29

3 Answers 3

0

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

Sign up to request clarification or add additional context in comments.

Comments

0

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()")

5 Comments

To get an element by id don't you need to see the id label? I don't see that element listed on the page. I see type="" and value="" but not id. Do I misunderstand something?
@JeremyTourville I was under the impression you created the website that you are interfacing with. My mistake.
@JeremyTourville There are 3 methods. Did you try the document.forms(0)... way?
I tried the document.forms(0) and frmMain method. When I tried the get element by id method there was an error - "expression expected" on the line -If ele.TagName.ToLower() == "input" AndAlso ele.Id.ToLower() == "btnSubmit" Then I did post my code as an answer above just so you could see it. Everything works but the button click.
yea that was a typo from converting the c# to vb code. == isn't valid in vb.net. Needed to be =.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.