C # WebBrowser How can I get innerHTML from a span inside a div

2

OI people.

I need to get with c # the innerHTML of the first SPAN element that has a numbered value that is inside a div and I I already wrote a code that is on the right track I think .. so far is this:

             HtmlElementCollection Elems;
              WebBrowser WebOC = webBrowser;
              Elems = WebOC.Document.GetElementsByTagName("div");

              foreach (HtmlElement elem in Elems) {

                  if ((elem.GetAttribute("id") == "MyId")) {


                  }         
              }

To do this with JavaScript I can with this code below

HTML

<div id="myId" class=""><span>874.005.877-81</span><span class="clipboard-copy"></span></div>

JAVASRIPT

var x = document.getElementById("myId");
var val = x.querySelector("span").innerHTML;
alert(val);

Thanks for any solution to this; D

    
asked by anonymous 20.08.2018 / 00:40

1 answer

0

In a perfect scenario, you just have to pick up your id, and then the first span element after the document is loaded:

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    string html = webBrowser1.Document.GetElementById("myId").GetElementsByTagName("span")[0].InnerHtml;
}

But there are other factors that can change this:

The page is not fully loaded in the DocumentCompleted event (uses ajax for example).

It may or may not have the element with its ID = myId. You should check for existence.

It may or may not have elements span within the selected element. You should check if it contains.

    
20.08.2018 / 02:13