Zander Brosky Zander Brosky - 2 years ago 66
HTML Question

How to convert an IEnumerable<HtmlElement> to a regular HtmlElement

I'm trying to use the

control in VS 2013 to enter text on a webpage. My app is written in
. The Html for the webpage doesn't use standard ids, but instead it uses something called a
. I believe this is custom to the company that built the webpage, but I'm not familiar enough with JavaScript to know for sure. This means I can't use the
method to point my app at the specific text box. The Html element is reproduced below:

<div class="rcPoint rcPointtext rcPointEditable" data-gel-id="gel_20" style="position: absolute; left: 25.9375%; top: 28.125%; font-size: 12%;">

I am trying to use the code snippet below to get the Html element by className. The code is based on a snippet from an MSDN forum post.

static IEnumerable<HtmlElement> ElementsByClass(HtmlDocument doc, string className)
foreach (HtmlElement e in doc.All)
if (e.GetAttribute("className") == className)
yield return e;

However, when I try to assign the output of this method to an
like so:

HtmlElement txtbox = ElementsByClass( doc2, "rcPoint rcPointtext rcPointEditable");

I get an error saying that you can't explicitly convert an IEnumerable to an HtmlElement.

So I have a few questions:

  1. Should I try to convert the
    to an Html element? And if
    so, how?

  2. Am I using the code snippet correctly? Are my arguments legit?

  3. Is this even the best way to point my app at the text box?

Thank you very much for your help and let me know if there's anything else I can post to help with the answer.

Answer Source

If you know the "custom id", you should probably try something like this

var gelId = "gel_20"; // This is the id you know
var txtbox = ElementsByClass( doc2, "rcPoint rcPointtext rcPointEditable")
             .FirstOrDefault(x => x.GetAttribute("data-gel-id") == gelId);
if (txtbox != null) {
    // Found textbox do something with it
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download