How would I get text from HTML on a website?

This is all I know to get to it but its only the elements;

webBrowserMain.Document.GetElementById("footer").Children

GetElementsByTagName("ptz-footer")

GetElementsByName("ptz_value")

The ptz_value has the text I want to retrieve.

Thanks...

Dani AI

Generated

was on the right track: the element named ptz_value comes back as a collection, and the next step is to read the actual text/value from the returned HtmlElement(s). Common pitfalls to watch for are: accessing the DOM before the page is finished loading, treating a form control like a normal element (inputs store their text in the value attribute, not InnerText), and elements that are generated later by JavaScript or live inside frames.

A compact, robust pattern is to get the collection, loop it, and pick the right property by tag type (input/textarea/select -> value, otherwise InnerText). Example (different from the snippet already in the thread):

var nodes = webBrowserMain.Document.GetElementsByName("ptz_value");
foreach (HtmlElement el in nodes)
{
    string tag = (el.TagName ?? "").ToUpperInvariant();
    string text = (tag == "INPUT" || tag == "TEXTAREA" || tag == "SELECT")
        ? el.GetAttribute("value")
        : el.InnerText ?? el.GetAttribute("value") ?? "";
    text = text.Trim();
    // use text
}

If ptz_value is nested inside a custom tag (for example a <ptz-footer>), first find the wrapper with GetElementsByTagName(...) and then call GetElementsByName(...) on each wrapper element. If the page builds content after load (AJAX or web components), either wait for that script to finish or run a small page script and return the result via HtmlDocument.InvokeScript (useful also when nodes are inside iframes or shadow roots that the WinForms DOM API cannot traverse).

See the .NET docs for the DOM helpers used above: , HtmlElement.GetAttribute / InnerText, and the WebBrowser.DocumentCompleted event for timing.

Recommended Answers

All 2 Replies

The GetElementsByName("ptz_value") return a NodeList object (collection of nodes). Traverse that collection and I'm sure you will get "Text".

Can you show an example? Iv'e been playing around with trying to get the text for some time now.

This is all I got.

this.Text = webBrowserMain.Document.GetElementById("ptz_value").InnerText;
                foreach (HtmlElement i in webBrowserMain.Document.GetElementById("footer").Children)
                {
                    
                    i.Document.
                     // MessageBoxEx.Show(i.GetElementsByTagName("ptz-footer").GetElementsByName("ptz_value").ToString());
                    
                }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.