GetAttribute, GetElementByID, GetElementByTagName
Posted: Fri Mar 11, 2011 7:37 pm
Hello and welcome to my tutorial for getting elements from webbrowser.
*NOTE* In order for this to work you need a webbrowser on your form.
GetAttribute
Q: Let's say a website has a button or text field that doesn't have an ID, how can we access it?
A: Well its easy, you use this code and change it to fit your needs:
GetElementByID
Q: I have a text field in the website and i want to enter text to it.
A: Well there are 2 ways of entering in the text depending on how the code looks like.
If its a <input type="text"> and has a ID then you will need this code:
GetElementByTagName
Q: I don't know the id of the element, and i want an alternative way then first part.
A: Well, then you can use GetElementByTagName, its similar to first part, only little differences:
and like the other parts, change "value" and "testing..."
BTW: This was not made for advanced coders ;)
Thank You for reading my tutorial
+rep if you liked it
- CodexVideos
*NOTE* In order for this to work you need a webbrowser on your form.
GetAttribute
Q: Let's say a website has a button or text field that doesn't have an ID, how can we access it?
A: Well its easy, you use this code and change it to fit your needs:
Code: Select all
Replace the "id" with other tags, like "class", "name", "tabindex", etc. and you replace "email" with the value.Dim wbelements As HtmlElementCollection = WebBrowser1.Document.All
For Each webpageelement As HtmlElement In wbelements
If webpageelement.GetAttribute("id") = "email" Then
'Do something here
'MsgBox("Element Found")
End If
Next
GetElementByID
Q: I have a text field in the website and i want to enter text to it.
A: Well there are 2 ways of entering in the text depending on how the code looks like.
If its a <input type="text"> and has a ID then you will need this code:
Code: Select all
But if its a <textarea> and also has a ID then you will need this:
WebBrowser1.Document.GetElementById("email").SetAttribute("value", "your email")
Code: Select all
But if they don't have a ID then follow the first part of the tutorial (GetAttribute)WebBrowser1.Document.GetElementById("email").InnerText = "your email"
GetElementByTagName
Q: I don't know the id of the element, and i want an alternative way then first part.
A: Well, then you can use GetElementByTagName, its similar to first part, only little differences:
Code: Select all
Just change the "input" to your tag name. ie. if its <div> then write "div"Dim elements As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
For Each selectedElement As HtmlElement In elements
selectedElement.SetAttribute("value", "testing...")
Next
and like the other parts, change "value" and "testing..."
BTW: This was not made for advanced coders ;)
Thank You for reading my tutorial
+rep if you liked it
- CodexVideos