Home > database >  How to display the content of a TextArea from the HtmlDocument of a WebBrowser Control
How to display the content of a TextArea from the HtmlDocument of a WebBrowser Control

Time:11-25

I have a TextArea in a page of my Site, I want to display the text of this TextArea, using the Document of a WebBrowser Control, to a Label.

This is the TextArea definition:

<textarea  id="bar" name="saisie"style="height: 260px; width: 700px;"> simple text</textarea>

How can I do it? I have this code:

WebBrowser1.Document.GetElementById(label1.text).GetAttribute("value", "bar")`

CodePudding user response:

The Element you describe has no ID, so you have to resort to the className of both the TEXTAREA and its DIV parent (to narrow down the chance to get the wrong element).

<div ><textarea rows="3" cols="80" >text1</textarea></div>

You can use GetElementsByTagName() instead of GetElementsById(), then filter the results

Something like this:

Imports System.Linq
' [...]

dim innerText = String.Empty
dim textArea = WebBrowser1.Document.GetElementsByTagName("TEXTAREA").
    OfType(Of HtmlElement)().
    FirstOrDefault(Function(elm) elm.GetAttribute("className") = "Label" AndAlso 
        elm.Parent.GetAttribute("className") = "labelapper")
if textArea IsNot Nothing Then
    innerText = textArea.InnerText
End If

EDIT: The HtmlElement in the original post has been changed, now showing an ID

dim textArea = WebBrowser1.Document.GetElementById("bar")
if textArea IsNot Nothing Then
    label1.Text = textArea.InnerText
End If
  • Related