2

So I have been using things like this:

webBrowser1.Document.GetElementById("month").SetAttribute("value", exp1);

Which allows me to set values, but now I want to grab a value instead of replacing it.

<div class="contents">
<div class="background">stuff</div>
<div class="content">
<h2>title</h2>
<p>
Blah blah number is <b>0100000</b>
<p>
</div>
</div>

How can I grab the number inside the tag that's inside the content class? Kind of stuck!

Thanks!

Vaughan Slater
  • 115
  • 1
  • 2
  • 8

3 Answers3

3

It's the code you need:

string theText;
foreach (HtmlElement item in webBrowser1.Document.GetElementsByTagName("div"))
        {
            if (item.GetAttribute("className") == "content")
                theText = item.GetElementsByTagName("b")[0].InnerText;
        }
Mehrdad
  • 2,054
  • 3
  • 20
  • 34
  • webBrowser1.Document.GetElementsByTagName("div").Cast().Where(p => p.GetAttribute("className") == "content").First().GetElementsByTagName("b")[0].InnerText – Mehrdad May 04 '16 at 10:17
  • @VaughanSlaterit No, it works on the html you provided in the question – Mehrdad May 04 '16 at 10:35
  • Sorry, I missed a few things (because I'm stupid) in the html. I updated the question with the full html.. Would that be why it's not working for me? Sorry! – Vaughan Slater May 04 '16 at 10:41
0

You can always get InnerText or InnerHtml of a chosen tag. Try this answer: https://stackoverflow.com/a/2958449/1786034

Community
  • 1
  • 1
zmechanic
  • 1,842
  • 21
  • 27
  • I'm looking to get the value from the from inside a class though and not and ID. I've tried the ones in the questions but no luck.. – Vaughan Slater May 04 '16 at 10:10
  • You said that you don't know how to get a content of a tag. That is a correct answer for your question. But it turns out that you don't know how to get `` tag either. For this you need `GetElementsByTagName("b")[0]`. – zmechanic May 04 '16 at 10:27
0

As explained in stackoverflow question

document.getElementById('id').getElementsByTagName('b').firstChild.nodeValue
Community
  • 1
  • 1
idipous
  • 2,868
  • 3
  • 30
  • 45