-1

Just joined here and started learning vb.net. I searched around, but didn't come across anything. Using Vb.net in Visual Studio with WebBrowser. I have a webpage with a button that I want to click on. The button is displayed 9 times, with the same code. So basically the identical button is displayed multiple times. When I create a function in vb.net to click on the button, it opens up all 9 at once. I would like to open only one at a time. I know in imacros you can get around this problem by assigning position 1, position 2, etc so it clicks on the first occurring one, then the second, etc. Is there a way to do this in vb.net? Thank you.

Here is the code that clicks the button(s)

Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
TextBox7.Text = TextBox7.Text + 1

theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
For Each curElement As HtmlElement In theElementCollection
If InStr(curElement.GetAttribute("value").ToString, "Visit") Then

    curElement.InvokeMember("Click")
    curElement.InvokeMember("MouseDown")
    curElement.InvokeMember("MouseUp")
    curElement.InvokeMember("OnClick")
    curElement.Focus()
End If

Next
Timer2.Interval = TextBox6.Text

End Sub

Here is the inspect element info of the buttons:

<input class="submit" value="Visit" style="width: 110px; height: 80px; border-radius: 40px; font-size: 27;" type="submit">
11
  • What do you mean "it opens up all 9 at once"? How specifically does this loop behave and how does it differ from what you're trying to do? Commented Oct 19, 2014 at 11:29
  • There are 9 buttons with the same code, when I initiate the click event, 9 buttons are clicked and 9 windows are opened. Since the buttons are the same, this happens. Since I cannot differentiate the buttons based on tag, value, name, etc, the only way to distinguish them would be based on their position (I'm assuming). I am looking to only click one at a time. -Thanks for reply Commented Oct 19, 2014 at 12:34
  • Well, the code posted clicks all the buttons in a loop. If you only want to click one button, then remove the loop and only click one button. I don't really understand what the problem is. Commented Oct 19, 2014 at 12:35
  • If I try to click one, all buttons are clicked because they all have the same code. They are identical in html inspect element values and on the same page. Commented Oct 19, 2014 at 12:46
  • What do you mean by "they all have the same code"? If they all do the same thing, why does it matter which one is clicked? If they do different things, only click one of them (as in, don't use a loop over all nine of them). If they're all truly identical, then in order to identify one of them you'll either need to add something to them to differentiate between them (such as an id attribute) or you'll need to assume their identity by their position in the array from GetElementsByTagName (which isn't necessarily reliable). Commented Oct 19, 2014 at 12:49

1 Answer 1

0

In a comment above, you state:

That's exactly what I am attempting to do, to invoke a click on one of them. I don't see how that is possible

Well, the code posted in the question is invoking the click event in a loop on all matching elements:

theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
For Each curElement As HtmlElement In theElementCollection
    ' invoke the click handler
Next

If you want to invoke the click event on only one element instead of all of them, then don't use a loop:

theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
If theElementCollection.Count > 0
    ' invoke the click handler on the first element
End If

However, since not all of the elements returned by your query are the ones you're looking for, the quickest approach for the current code would likely be to just introduce an Exit For statement after the first target element is reached. Something like this:

theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
For Each curElement As HtmlElement In theElementCollection
    If InStr(curElement.GetAttribute("value").ToString, "Visit") Then
        curElement.InvokeMember("Click")
        curElement.InvokeMember("MouseDown")
        curElement.InvokeMember("MouseUp")
        curElement.InvokeMember("OnClick")
        curElement.Focus()
        Exit For ' right here
    End If
Next

That way the loop will stop iterating after the first matching element is "clicked".

1
  • Ah thanks David (Sorry for the question from hell) The Exit For was what I was looking for. Thanks a million! Commented Oct 19, 2014 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.