0

vb.net 2019 - webbrowser control

So I've isolated a web element to click before hand, so my program captures the entire element text, and iterates through a for loop to find match and clicks it on a webbrowser.

 For Each element As HtmlElement In Form1.webclient.Document.All
        If element.OuterHtml.ToString = itemchunk Then
            element.InvokeMember("Click")
        End If
 Next

itemchunk can equal something like this:

<span>I'm Feeling Trendy</span>

or

<input name="btnK" class="gNO89b" aria-label="Google Search" type="submit" value="Google Search" data-ved="0ahUKEwihndSp3PPoAhUEUK0KHS7gAfMQ4dUDCAw">

or pretty much any other element you can think of

It's not feasible to click the element during the initial itemchuck building.

I know that with something that has an ID or Name can be accessed quickly and directly, but is there a faster way to locate a known element in a webbrowser other than using the above for loop? (ie I'm Feeling Trendy)

should I use a background worker while it iterates through hundreds of elements? (it does hang me up during the loop, so i probably should anyways)

is it actually faster to break down and classify elements before hand? (leaving you with a smaller group to iterate though)

Anyone come up with something quicker?

(keep in mind the elements were already isolated before hand and could be clickable, and is later matched up on a refreshed page)

edit: i've also tried keeping track of the element from point feature, but it doesn't seem to work?

anyone know if the "element from point" values change if the browser is scrolled or resized?

drpepper1324
  • 113
  • 9
  • 1
    Read the notes here: [How to get an HtmlElement value inside Frames/IFrames?](https://stackoverflow.com/a/53218064/7444103). Note that: the WebBrowser parser builds collections of element types for its own functionality: you can filter these collections using the `.Document.GetElementsByTagName()` method. Since it's an enumerable collection, you can add a `Where()` condition, further filtering by, e.g., Attribute. Then, don't use `OuterHtml` if not strictly necessary and don't compare strings. As shown, use `GetHashCode()` to compare hashes, then the string, if the hash matches. – Jimi Apr 19 '20 at 07:34
  • 1
    Some collections are already provided by name: Forms, Images and Links. --- You should probably parse `Document.Body` instead of `Document.All`, to narrow down list of elements --- As described in the notes I linked, the main Window container can be composed of multiple sub-document: each IFrame has its own, a Frames can have one, so you need to be prepared to parse sub-documents, too. The element you're looking for may not be found in the main Window Document. – Jimi Apr 19 '20 at 07:40
  • I'll try to implement the method described. my minds already turning to mush tonight. I'll get back to you soon. – drpepper1324 Apr 19 '20 at 10:19

0 Answers0