Get value of input in vb.net webbrowser? - vb.net

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.

Related

GetElementById Two same ID's

Hey I need help well im trying to make a program that uses a site to do some stuff and it has two of the same id's for both textboxs im trying to input into my code is WebBrowser2.Document.GetElementById("form-control-3").InnerText = BunifuMaterialTextbox1.Text. Well anyway im tryint to do that to input into the first textbox and the 2nd textbox has the same id and classname so i don't really know what to do.
You could try grabbing all the HTML elements based on the tag and then loop through to see if the id matched. Something like...
Dim Elems As HtmlElementCollection
Elems = WebBrowser2.Document.GetElementsByTagName("[tagName]")
For Each elem as HtmlElement in Elems
Dim idStr As String = elem.GetAttribute("id")
If ((idStr IsNot Nothing) And (idStr = "form-control-3"))
elem.InnerText = BunifuMaterialTextbox1.Text
End If
Next
i don't know if this help but you can do a input array
example:
<input name="name[]">
and...
document.getElementsByName("name[]")

Pull webpage information to text box

So what I am trying to do is pull some data from a webpage, and put it into a text box. Online, I can find alot about doing it the other way around (putting text box into web) but none the way I want.
I have gotten this far... (not very) and have no clue where to go from here
Dim Helement As HtmlElement
Helement = WebBrowser1.Document.GetElementById("txtAddress1")
Helement.GetAttribute()
Here is the element from the browser I am trying to use
input#ctl00_m_g_79f90d63_a5eb_43e1_9403_69fefa0ba004_ctl00_txtAddress1
Thanks guys!
This is what I tried:
Dim Helement As HtmlElement = WebBrowser1.Document.GetElementById("txtAddress1")
Dim elementtype As String = Helement.GetAttribute("text")
Dim textContents As String = Helement.InnerText
Address.Text = textContents
The GetAttribute function will return the value of an attribute of the element you retrieved using GetElementById.
For example, something like:
<input type="text" id="txtAddress1">
Dim Helement as HtmlElement = WebBrowser1.Document.GetElementById("txtAddress1")
Dim elementType as String = Helement.GetAttribute("type")
Dim textContents as String = Helement.InnerText
InnerText MSDN
GetAttribute MSDN

Put text into a textbox from a website in VB.NET

I want to know how to put text to a website's textbox with VB.NET.
Like when you complete a textbox and press enter.
Something like this:
Dim web as webbrowser
web.document.textbox(1).insert("text")
it would look something like this:
WebBrowser1.Document.GetElementById("login_password").SetAttribute("value", TextBox2.Text)
As giuseppe has suggested, you need to search for the element in the document and then set it's value property. for example following code logins into website by setting its userId and password textboxes and clicking the submit button.
Dim htmlDoc=WebBrowser1.Document
Dim elem_Input_UsrName As HtmlElement = htmlDoc.GetElementById("username")
Dim elem_Input_PssWrd As HtmlElement = htmlDoc.GetElementById("password")
Dim elem_Input_Submit As HtmlElement = getElementByClassName("Submit", "Input", htmlDoc)
If elem_Input_UsrName IsNot Nothing AndAlso elem_Input_PssWrd IsNot Nothing Then
elem_Input_UsrName.SetAttribute("value", "xyz#ABC.com")
elem_Input_PssWrd.SetAttribute("value", "yourpassoword")
elem_Input_Submit.InvokeMember("click")
End If
This will help 100%:
webbrowser1.document.getElementById("yourtextboxidinwebsite").innertext = textbox1.text
I get a way that if u want to receive from the website itself another way I need to receive results from the website into the text box. U can use this method.
first, make a web browser to load the page
WebBrowser1.Navigate("https://tools.keycdn.com/geo")
then this
TextBox1.Text = WebBrowser1.Document.GetElementById("geoResult").InnerText

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

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