Programatically clicking an html button by vb.net - vb.net

I have to click a HTML button programatically which is on the 3rd page of the website . The button is without id. It has just name type and value . The HTML code of the button is given below
<FORM NAME='form1' METHOD='post' action='/dflogin.php'>
<INPUT TYPE='hidden' NAME='txtId' value='E712050-15'>
<INPUT TYPE='hidden' NAME='txtassId' value='1'><INPUT TYPE='hidden' NAME='txtPsw' value='HH29'>
<INPUT TYPE='hidden' NAME='txtLog' value='0'><h6 align='right'>
<INPUT TYPE='SUBMIT' NAME='btnSub' value='Next' style='background-color:#009900; color:#fff;'></h6>
</FORM>
i am using the following code to click it
For Each webpageelement As HtmlElement In allButtons
If webpageelement.GetAttribute("value") = "Start" Then
webpageelement.InvokeMember("click")
End If
Next
But i cant able to click it . I am using the vb.net 2008 platform. Can anyone tell me the solution to click it?

Try invoking Submit on the Form rather than click on the Input.
EDIT: Oops, HTMLElementCollection does not implement the generic IEnumerable. Try this instead:
Dim l_forms = WebBrowser1.Document.GetElementsByTagName("form")
If l_forms.Count > 0 Then
l_forms.Item(0).InvokeMember("submit")
End If

Dim Elems As HtmlElementCollection
Dim WebOC As WebBrowser = WebBrowser1
Elems = WebOC.Document.GetElementsByTagName("input")
For Each elem As HtmlElement In Elems
elem.InvokeMember("click")
Next

I spent quite a while trying to find an answer to this. I never realized that you can invoke a click using javascript. Once I read that the solution becomes very easy:
Public Function clickbyid(ByVal id)
If TheBrowser.Document.GetElementById(id) IsNot Nothing Then
Dim Headers As String = "" 'insert headers if you want to
TheBrowser.Navigate("javascript:document.getElementById('" & id & "').click();", "_self", Nothing, Headers)
'This keeps the function active until the browser has finished loading
Do While TheBrowser.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
Loop
Return 1
Else
MessageBox.Show("Could not find link by id" & id)
Return Nothing
End If
End Function
You may have to change "TheBrowser" to "WebBrowser1".
If you don't want to return a result then simply change it to public sub and remove the return statements.
If the htmlelement does not have an id then use the following functions to add one
Public Function clickbyelement(ByVal theButton As HtmlElement)
Try
'Generate a unique id to identify the element
Dim randomID = "vbAdded" & GetRandom(10000, 100000).ToString
'check to make sure the ID does not already exist
While TheBrowser.Document.GetElementById(randomID) IsNot Nothing
randomID = "vbAdded" & GetRandom(10000, 100000).ToString
End While
'add the ID to the element
theButton.SetAttribute("id", randomID)
'click
clickbyid(randomID)
Return True
Catch ex As Exception
Return False
End Try
End Function
Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
' by making Generator static, we preserve the same instance '
' (i.e., do not create new instances with the same seed over and over) '
' between calls '
Static Generator As System.Random = New System.Random()
Return Generator.Next(Min, Max)
End Function
Thanks to Dan Tao for the random number: https://stackoverflow.com/a/2677819/381273
Tested and working!

It sounds like you are trying to do a client-side action - click the button when operating on the server (VB code). This will not work.
So, either use Javascript document.form1.submit() or call the VB sub that is connected to the button btnsub.click (or whatever you named it).

The Solution:
WebBrowser1.Navigate("UrlHere!")
'wait till the page is laoded
Do While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
Loop
WebBrowser1.Document.Body.InnerHtml = Replace(WebBrowser1.Document.Body.InnerHtml, "NAME='btnSub'", "NAME='btnSub' id='btnSub'") 'insert id into youre button
WebBrowser1.Document.GetElementById("btnSub").InvokeMember("click") 'click on btnSub
;)

Related

How do I create a new form instance whose name I have in a string? [duplicate]

Code to create new form instance of a closed form using form name
I want to replace the long Select Case list with a variable.
Full code of module
In Access 2010 I have a VBA function that opens a new instance of a form when given a string containing the form's name. By adding a form variable "frm" to a collection:
mcolFormInstances.Add Item:=frm, Key:=CStr(frm.Hwnd)
The only way I can figure out to open "frm" is with a Select Case statement that I've manually entered.
Select Case strFormName
Case "frmCustomer"
Set frm = New Form_frmCustomer
Case "frmProduct"
Set frm = New Form_frmProduct
... etc ... !
End Select
I want it to do it automatically, somewhat like this (although this doesn't work):
Set frm = New Eval("Form_" & strFormName)
Or through some code:
For Each obj In CurrentProject.AllForms 'or AllModules, neither work
If obj.Name = strFormName Then
Set FormObject = obj.AccessClassObject 'or something
End If
Next
Set frm = New FormObject
I just want to avoid listing out every single form in my project and having to keep the list updated as new forms are added.
I've also done some testing of my own and some reading online about this. As near as I can tell, it isn't possible to create a new form object and set it to an instance of an existing form using a string that represents the name of that form without using DoCmd.OpenForm.
In other words, unless someone else can prove me wrong, what you are trying to do cannot be done.
I think you are looking for something like this MS-Access 2010 function. (The GetForm sub is just for testing):
Function SelectForm(ByVal FormName As String, ByRef FormExists As Boolean) As Form
For Each f In Application.Forms
If f.Name = FormName Then
Set SelectForm = f
FormExists = True
Exit Function
End If
Next
FormExists = False
End Function
Sub GetForm(ByVal FormName As String)
Dim f As New Form
Dim FormExists As Boolean
Set f = SelectForm(FormName, FormExists)
If FormExists Then
MsgBox ("Form Found: " & f.Caption)
Else
MsgBox ("Form '" & FormName & "' not found.")
End If
End Sub
Here's an ugly hack I found:
DoCmd.SelectObject <acObjectType>, <YourObjectsName>, True
DoCmd.RunCommand acCmdNewObjectForm
The RunCommand step doesn't give you programmatic control of the object, you'll have to Dim a Form variable and Set using Forms.Item(). I usually close the form after DoCmd.RunCommand, then DoCmd.Rename with something useful (my users don't like Form1, Form2, etc.).
Hope that helps.

How do I save textboxes with tabs depending which tab is open?

I am trying to create a document writer in vb.net
So I decided to add a tab control, so I can create a new tab when I press a button. I would like to save the text in the tab that is open. I created a function the returns the text depending on what tab is open, but that only works for the two default tabs. I don't know how I would save if I've created a new tab.
The function to get the text is this:
Public Function getText() As String
If tabPage1.Visible = True Then
Return mainText.Text
ElseIf tabPage2.Visible = True Then
Return textBox1.Text
End If
End Function
I've done some research online, I've even looked at SharpDevelop's source code and I couldn't find anything.
Thanks in advance!
EDIT :
Public Sub setText(Byval value As String)
If tabPage1.Visible = True Then
mainText.Text = value
ElseIf tabPage2.Visible = True Then
textBox1.Text = value
End If
End Sub
Does anyone know how I would do an open feature determined on what tab is open (as before.)
If I understand you correctly, you are trying to have a textbox in each of your tabPages generated dynamically. If this is the case you could generalize your GetText function with this code
Function GetText() As String
If tabControl1.SelectedTab IsNot Nothing Then
Return tabControl1.SelectedTab.Controls.OfType(Of TextBox)().First().Text
End If
End Function
This requires that you have at least one textbox in each page (and your TabControl is named tabControl1). The SelectedTab property (if not nothing) is the current tabPage displayed by your tabControl

Get value of input in vb.net webbrowser?

I have the following input, I want to get it value using vb.net WebBrowser, and then put it into a variable how can i perform that?
<input type="hidden" name="loginurl" value="https://example.com?ctoken=KWYZlCrYxt">
I'm trying to use this code, but i don't know how to put the value i got into a variable:
For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("input")
'Depending on how the source code is formatted on the tag, you may also try Element.OuterHTML, Element.InnerText and Element.OuterText in the line below
If Element.OuterHtml.Contains("name=""loginurl""") Then
Element.GetAttribute("value")
Dim login = value
WebBrowser1.Navigate(login)
Exit For
End If
Drag and drop webbrowser control and use the following snippet
WebBrowser1.Navigate("https://example.com?ctoken=KWYZlCrYxt")
Here is a good reference
Hope this is useful
I'd say just assign a variable with Dim attributeValue = Element.GetAttribute("value")... am I missing something obvious here?
For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("input")
'Depending on how the source code is formatted on the tag, you may also try Element.OuterHTML, Element.InnerText and Element.OuterText in the line below
If Element.OuterHtml.Contains("name=""loginurl""") Then
Dim attributeValue = Element.GetAttribute("value")
Dim login = value
WebBrowser1.Navigate(login)
Exit For
End If
Try:
Dim AllElementes As HtmlElementCollection = WebBrowser1.Document.All
For Each webpageelement As HtmlElement In AllElementes
If webpageelement.GetAttribute("name") = "loginurl" Then
Dim login = webpageelement.GetAttribute("value")
WebBrowser1.Navigate(login)
End If
Next
should work.

Webbrowser Click Button With No ID Or Name

I am trying to use the WebBrowser in Visual Studio 2012 (Visual Basic) to click a button. I have done a lot of research, but all I can find is to click with an ID or a name.
Here is the HTML code.
<button type='submit' class="btn btn-default" style='float:right'>
Login<i class="gicon-chevron-right"></i>
I am going to have the webbrowser hidden, & then when the user clicks Button1, it invokes the click on the webbrowser. Please help.
You need to search for your element one by one. Try something like this:
For Each elem As HtmlElement In wb1.Document.GetElementsByTagName("button")
' Check the attributtes you want
If elem.GetAttribute("class") = "btn btn-default" Then
'Check even the text if you want
If elem.InnerText = "Login" Then
'Invoke your event
elem.InvokeMember("click")
End If
End If
Next
This will search on all the button elements on the document. You could narrow the searching by looking inside some element that contains your button that has ID or something:
Dim myElem As HtmlElement = wb1.Document.GetElementById("myContainerId")
For Each elem As HtmlElement In myElem.GetElementsByTagName("button")
'...
Next
simple code :
WebBrowser1.Document.Forms(0).InvokeMember("submit")
Dim HtmlElementcoll As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
For Each elem As HtmlElement In HtmlElementcoll
' Check the attributtes you want
If elem.GetAttribute("value") = "Sign In" Then
'Check even the text if you want
' If elem.InnerText = "Sign In" Then
'Invoke your event
elem.InvokeMember("click")
'End If
End If
Next
i have modified the code above and its working add HtmlElementCollection and you are good to go..

Webkit Autologin not working using Document.GetElementsByTagName("input")

I really need to get this autologin script working with Webkit 0.5 in Visual Basic:
It is intended to run on https://www.ea.com/profile/login
Somehow it doesn't seem to work as I cannot
Dim ElementListe As HtmlElementCollection
Therefore the following is impossible:
For Each Element As HtmlElement In ElementListe
Current Code (not mine):
Private Sub Load()
AlleFelderAusgefüllt = False
Try
BattlelogBrowser.Navigate("https://www.ea.com/profile/login")
Catch ex As Exception
Beep()
End Try
Do While BattlelogBrowser.IsBusy = True
Application.DoEvents()
Loop
Dim ElementListe
ElementListe = WebkitBrowser1.Document.GetElementsByTagName("input")
For Each Element In ElementListe
Select Case Element.GetAttribute("name")
Case "email"
Element.SetAttribute("Value", TextBox1.Text)
Case "password"
Element.SetAttribute("Value", TextBox2.Text)
End Select
Next
For Each Element In ElementListe
If Element.GetAttribute("value").Equals("Login") Then
Element.InvokeMember("click")
End If
Next
End If
End Sub
Any solutions? It worked quite similar with the usual Webbrowser, hence I hoped one of you might be able to edit the code or even create a new one!
Regards, RiX
Use Fiddler or Wireshark to compare what goes over the wire when it works and when it doesn't work... once you know the differences you can change your accordingly...
This way you could be able to achieve what you want without the browser control by using HttpWebRequest...
Dim doc As WebKit.DOM.Document = WKB.Document
Dim inputs As WebKit.DOM.NodeList = doc.GetElementsByTagName("input")
Dim inputElement As WebKit.DOM.Element
For Each item As WebKit.DOM.Element In inputs
inputElement = DirectCast(item, WebKit.DOM.Element)
Select Case inputElement.GetAttribute("name")
Case "username"
inputElement.SetAttribute("Value", txtAccount.Text)
Case "password"
inputElement.SetAttribute("Value", txtPassword.Text)
End Select
Next
You can submit form by:
For Each item In inputs
inputElement = DirectCast(item, WebKit.DOM.Element)
If inputElement.GetAttribute("value").Equals("Login") Then
WKB.StringByEvaluatingJavaScriptFromString("document.getElementById('smlogin').click()")
End If
Next