Webbrowser Click Button With No ID Or Name - vb.net

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

Related

WebBrowser control Anchor tag target = _Blank opening outside WebBrowser control

I want to navigate to new tab when click on anchor. My code :
Hide Copy Code
Dim elements As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("a")
For Each pElem As HtmlElement In elements
If (pElem.GetAttribute("HREF") <> "" AndAlso pElem.GetAttribute("HREF") <> "#" Then
WebBrowser1.AllowNavigation = True
pElem.SetAttribute("target", "_Blank")
pElem.InvokeMember("click")
End If
Next
When i Perform Click it will open in IE Browser instead of Web-Browser Control. Please brief me what I am doing wrong?
FYI, Instead of Navigate using URL i want to Navigate Using click and its compulsory.
Thanks in advance.

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

How to click HTML button programatically by 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);

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

Acessing HTML buttons having the same names and values by vb.net

I am trying to access Some HTML buttons. I am using vb.net 2008 platform to click these buttons programatically . Problem is that the all 17 buttons are without id and have the same name type and value and are in a same form . So plz guide me how to click each button indivisually . Here is the HTML code of the these buttons.
lets say that the you got a webbrowser control
And The Html Is:
"<button id=namee>Click</button><button id=namee>Click</button><button id=namee>Click</button>"
you can change the id's of the butoons + click them:
WebBrowser1.Navigate("")
Do While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
Loop
WebBrowser1.Document.Body.InnerHtml = "<button id=namee>Click</button><button id=namee>Click</button><button id=namee>Click</button>"
For i = 1 To 3
WebBrowser1.Document.Body.InnerHtml = Replace(WebBrowser1.Document.Body.InnerHtml, "namee", "n" & i.ToString, , 1)
Next
WebBrowser1.Document.GetElementById("n1").InvokeMember("click")
WebBrowser1.Document.GetElementById("n2").InvokeMember("click")
WebBrowser1.Document.GetElementById("n3").InvokeMember("click")
there is another way:
Dim Elems As HtmlElementCollection
Dim WebOC As WebBrowser = WebBrowser1
Elems = WebOC.Document.GetElementsByTagName("button")
For Each elem As HtmlElement In Elems
elem.InvokeMember("click")
Next