Rather getting elements by ID then, name, can I just use this as a generic to all?

Joined
Apr 13, 2011
Messages
19
Programming Experience
Beginner
I am building an app where there will be several instances where ID's are going to be listed and then several instances where their will be tagnames, classes.
Rather than having to using the GetElementbyID for each one that requires an ID name and another chunk of code for GetElementByTagName can I use something like
VB.NET:
Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
For Each webpageelement As HtmlElement In allelements
If element.getattribute("ID")= "Cloud" then
Element.setAttribute("value", "sky")
End If

Would something like this work as a template for all elements where I would just replace the getattribute type to name, or , id, or even class?

Thanks
 
Yes, that can be done.
 
Cool...Thanks John....another question just came up.....if I needed to grab more than one element on the page...say....sky, grass, moutain...do I just set a loop for the above code?

Loop
Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
For Each webpageelement As HtmlElement In allelements
If element.getattribute("ID")= "Cloud" then
Element.setAttribute("value", "sky")
Next
End If
 
That would depend on the circumstances, which I don't know. Using loops and reusing code by putting them in methods that can be called with different parameters is very common.
 
Okay here would be an example. Lets say I navigate to a web page and on that page I need to fill out 6 textboxes on the screen.

The first one element is an ID = "clouds"
The second element is tagname = "grass"

What I need to do is to loop through those tag names and pass my information to the textboxes on the site.

I am able to do them by just using GetElementByID then/or by TagName but I was thinking there had to be a better method with less coding by just copying the info above into a template and just changing the "type" and "name" and setting it to my value. If that makes any sense
 
Yeah, as I said, you should be having a function method with parameters where you can provide different arguments to supply the information about the element you want. For example a method like this:
VB.NET:
Private Function GetElement(attribute As String, value As String) As HtmlElement
Within this method you loop the elements and check if the given attribute has the given value and return the element.
Now you can call this method repeatedly to get elements of various criteria.
 
Back
Top