How to click HTML button programatically by vb.net - vb.net

I am trying to click a button programatically . The button is on the 3rd page of the website. and it does not have any 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 have tried these codes in vb.net 2008 express edition...
For Each webpageelement As HtmlElement In allelements
If webpageelement.GetAttribute("value") = "Next" Then
webpageelement.InvokeMember("click")
End If
Next
And 2nd one
theElementCollection = WebBrowser1.Document.GetElementsByTagName("INPUT")
For Each curElement As HtmlElement In theElementCollection
ctrlIdentity = curElement.GetAttribute("innerText").ToString
If ctrlIdentity = "Next" Then
curElement.InvokeMember("click")
End If
Next
and 3rd one is
Dim l_forms = WebBrowser1.Document.GetElementsByTagName("form")
If l_forms.Count > 0 Then
l_forms.Item(0).InvokeMember("submit")
End If
and 4th one is
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
and the last one is
Dim i As Integer
Dim allButtons As HtmlElementCollection
allButtons = WebBrowser1.Document.GetElementsByTagName("input")
i = 0
For Each webpageelement As HtmlElement In allButtons
i += 1
If i = 5 Then
webpageelement.InvokeMember("click")
End If
Next
These all codes are unable to click that button. Kindly Please Suggest a appropriate solution for clicking this button. Thank You So Much

something like this would be a server side JavaScript and jquery solution
string script = "$(document).ready(function() {$('input[name=btnSub]').trigger('click'); });";
ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script, true);

Related

Entering text in text box inside a webbrowser

I am connecting to a website trough a webbrowser, then i want to post a message on a message board.
This is the HTML of the text box:
<div class="fr-element fr-view" dir="ltr" contenteditable="true" style="min-height: 100px;" aria-disabled="false" spellcheck="true"><p>TEXT GOES HERE</p></div>
I have tried the following 2 codes:
For Each CurrentElement As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")
If CurrentElement.GetAttribute("class") = "fr-element fr-view" Then
CurrentElement.InnerText = TextBox1.Text
End If
Next
For Each CurrentElement As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")
If CurrentElement.GetAttribute("class") = "fr-element fr-view" Then
For Each InnerCurrentElement As HtmlElement In CurrentElement.GetElementsByTagName("p")
InnerCurrentElement.InnerText = RichTextBox1.Text
Next
End If
Next
None of them does anything
I really havent even messed around with HTML before this at all even so I am not sure what I could search for to solve my issue as I am able to handle other text boxes, for example the login ones which are like this:
<input type="text" class="input" name="login" autofocus="autofocus" autocomplete="username" id="aaaaa">
You must to do that after your WebBrowser have finished to load the document. A little example below:
WebBrowser1.ScriptErrorsSuppressed = True
WebBrowser1.Navigate("https://html.com/tags/input/")
AddHandler WebBrowser1.DocumentCompleted, Sub(senderObj As Object, eObj As WebBrowserDocumentCompletedEventArgs)
Dim inputs As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
If inputs IsNot Nothing AndAlso inputs.Count > 0 Then
For Each current As HtmlElement In inputs
current.SetAttribute("value", "Eurekaaaaaaaaaaaaaaaaaaaaa")
Next
End If
End Sub

How to see if an object is blocked in Vb.net Webbrowser

I have a webbrowser in a form that I am trying to see, if the following two items being displayed or not
<div class="alert alert-danger bet" style="display: block;">You Lost</div>
<div class="alert alert-success bet" style="display: none;">You Won</div>
I have searched and I can not find a solution to this. I have found how to search for them and find them without them having an ID but i can not see the style. If possible i would like to put the style result in a textbox for now until i figure out how to deal with the outcome.
EDIT
Ok so here's where i am now
Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("DIV")
For Each curElement As HtmlElement In theElementCollection
If curElement.OuterHtml.Contains("alert alert-danger bet") Then
TextBox1.Text = curElement.GetAttribute("style")
End If
Next
This returns System._ComObject and it seems to be the only thing that i can get to return at all.
Any suggestions?
OK so finally I believe I have a solution.
Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("DIV")
For Each curElement As HtmlElement In theElementCollection
If curElement.OuterHtml.Contains("alert alert-danger bet") Then
If curElement.Style = "display: block;" Then
TextBox1.Text = "True"
ElseIf curElement.Style = "display: none;" Then
TextBox1.Text = "False"
End If
End If
Next
End If
Next
Now obviously this is only showing one of my items so I will still have to handle the other but at least this gives me a True or False statement.

Block a certain element in web browser Vb.net

I a wondering if and how you would be able to block a certain element in the webbrowser.
For example there is a certain element on a website:
<div class="btn" id="button" value="ClickMe"/>
Will you be able to block or hide that specific element in the webbrowser?
There might be a certain ad, iframe or whatever its called that might just be in the way?
Thanks for the help I appreciate it.
double click on webbrowser1 designer you should be in WebBrowser1_DocumentCompleted add this line
'selector by division id
webbrowser1.Document.getElementById("button").innerHtml = ""
'selector spesific element with spesific attribute
<input id="_authentication_token" name="_authentication_token" type="hidden" value="11740686"/>
dim mycol as htmlElementCollection = webbrowser1.document.getElementBytagname("input")
for each ele as Htmlelement in mycol
if (ele.getAttribute("value").tostring = "11740686") then ele.innerHtml = ""
next
for your exemple :
<div class="btn" id="button" value="ClickMe"/>
dim mycol as htmlElementCollection = webbrowser1.document.getElementBytagname("div")
for each ele as Htmlelement in mycol
if (ele.getAttribute("value").tostring = "ClickMe" and ele.getAttribute("id").tostring = "button" ) then ele.innerHtml = ""
next

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

Programatically clicking an html button by 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
;)