Question

HTML:

<input type="text" size="15" maxlength="79" value="" name="username">

As you can see, no ID. The HTML above is a textbox that i want to auto fill in whit my value as soon as i start the webpage whit my code.

this is what i found:

WebBrowser1.Document.Forms(0).GetElementsByTagName("username")(0).SetAttribute("value", (Text))

But whit this i get the error:

Value of '0' is not valid for 'index'. 'index' should be between 0 and -1.
Parameter name: index

What am i doing wrong?

Was it helpful?

Solution

This isn't going to find any elements:

WebBrowser1.Document.Forms(0).GetElementsByTagName("username")

"Tag name" doesn't mean the value of the name attribute, it means the name of the HTML tag itself. Like this:

WebBrowser1.Document.Forms(0).GetElementsByTagName("input")

Of course, this is likely to return multiple matched elements, so you'll need to further identify which one you want to modify. The point being that you should do some error checking to make sure that it finds anything, because trying to index an empty collection will result in an error:

WebBrowser1.Document.Forms(0).GetElementsByTagName("username")(0)

Since the collection has no elements, there is nothing at index 0.

OTHER TIPS

May be u could try

Me.WebBrowser1.Document.GetElementByName("username").SetAttribute("Value", txtUsername.Text)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top