Htmldocument's InnerText property returns nothing in Webbrowser, bugor why?

  • Thread starter Thread starter ralpugan
  • Start date Start date
R

ralpugan

Hello,

It may sound as a weird problem, when you use InnerText to "get" the
value of an element it returns nothing, but works fine when you set
text using InnerText.

Steps to reproduce: 1) Create a winform application having a
webbrowser placed on it and a button.
2) Navigate to "www.google.com" using
webbrowser1.Navigate or assigning its "url" property (up to you).
3) Highlight search textbox of Google.
(click on it)
4) In button's click event handler (or
whenever you want to get), try:


WebBrowser1.Document.ActiveElement.InnerText = "hello"

.....and it works. But if you want to GET typed text on search textbox,
it returns null or nothing:

MsgBox
(WebBrowser1.Document.ActiveElement.InnerText.ToString)
...doesn't work! (null reference exception).

Also that doesn't work though you search entire element collection by
specifiying name of element explictly:

MsgBox(WebBrowser1.Document.All.GetElementsByName("q").Item
(0).InnerText.ToString)

...which doesn't work also!


The only workaround i found is explictly specifiying its attribute
("value") as follows:
MsgBox(WebBrowser1.Document.All.GetElementsByName("q").Item
(0).GetAttribute("value").ToString)

....which works fine

So, why cannot InnerText property doesn't return value of a text
element on webbrowser, whereas it can set value fine?

Regards,

ralpugan
 
Hello,

It may sound as a weird problem, when you use InnerText to "get" the
value of an element it returns nothing, but works fine when you set
text using InnerText.

Steps to reproduce: 1) Create a winform application having a
webbrowser placed on it and a button.
                              2) Navigate to "www.google.com" using
webbrowser1.Navigate or assigning its  "url" property (up to you).
                             3) Highlight search textbox of Google.
(click on it)
                             4) In button'sclick event handler (or
whenever you want to get), try:

WebBrowser1.Document.ActiveElement.InnerText = "hello"

....and it works. But if you want to GET typed text on search textbox,
it returns null or nothing:

                           MsgBox
(WebBrowser1.Document.ActiveElement.InnerText.ToString)
 ...doesn't work! (null reference exception).

Also that doesn't work though you search entire element collection by
specifiying name of element explictly:

MsgBox(WebBrowser1.Document.All.GetElementsByName("q").Item
(0).InnerText.ToString)

..which doesn't work also!

The only workaround i found is explictly specifiying its attribute
("value") as follows:
      MsgBox(WebBrowser1.Document.All.GetElementsByName("q").Item
(0).GetAttribute("value").ToString)

...which works fine

So, why cannot InnerText property doesn't return value of a text
element on webbrowser, whereas it can set value fine?

Regards,

ralpugan

Shortly, InnerText property cannot get value, but it can set value
fine though you deal with same element, like in the example of Google:

That doesn't work to get text on search textbox:
MsgBox(WebBrowser1.Document.All.GetElementsByName("q").Item
(0).InnerText.ToString)

But that works to set a text on it:
WebBrowser1.Document.All.GetElementsByName("q").Item
(0).InnerText = "hello world"

Curious...

ralpugan
 
innerText should be for the text that is *inside* the HTML beding and end
tag, not for the value attribute so it looks it works as expected (ie you
ahve no innerText for a textbox).

If it works to set the "value" attribute for a texbox (you tried on the same
element or was it another HTML element ?), I would stay away from this as
this behavior doesn't look correct (at least to me)..


--
Patrice


"ralpugan" <[email protected]> a écrit dans le message de groupe de
discussion :
(e-mail address removed)...
 
innerText should be for the text that is *inside* the HTML beding and end
tag, not for the value attribute so it looks it works as expected (ie you
ahve no innerText for a textbox).

If it works to set the "value" attribute for a texbox (you tried on the same
element or was it another HTML element ?), I would stay away from this as
this behavior doesn't look correct (at least to me)..

--
Patrice

"ralpugan" <[email protected]> a écrit dans le message de groupe de
discussion :
(e-mail address removed)...

I tried on the same element and on various sites (like in the search
box of Google) and the behaviour is the same. It cannot get text in
textbox but it can set fine just with InnerText.I can set text fine
without using GetAttribute("value") but cannot get text on textbox on
document without GetAttribute("value") though i refer to same element
on same website with same property (InnerText).

That makes confusion.

why?

ralpugan
 
ralpugan
I tried on the same element and on various sites (like in the search
box of Google) and the behaviour is the same. It cannot get text in
textbox but it can set fine just with InnerText.I can set text fine
without using GetAttribute("value") but cannot get text on textbox on
document without GetAttribute("value") though i refer to same element
on same website with same property (InnerText).

That makes confusion.

why?

Looks like IE specific (works in IE using JavaScript, doesn't work with FF).
Perhaps someone from MS thought it was making sense as the text is seen
"inside" the textbox so it is not really within the begin/end tags. Who
knows ?

Anyway this is NOT what you want. You want to get/set an attribute, not the
innerText (which is the text within the begin and end tag and a textbox
doesn't have such as text) so IMO just don't use innerText to do that even
if by luck it does the same than what you want...
 
 > ralpugan






Looks like IE specific (works in IE using JavaScript, doesn't work with FF).
Perhaps someone from MS thought it was making sense as the text is seen
"inside" the textbox so it is not really within the begin/end tags. Who
knows ?

Anyway this is NOT what you want. You want to get/set an attribute, not the
innerText (which is the text within the begin and end tag and a textbox
doesn't have such as text) so IMO just don't use innerText to do that even
if by luck it does the same than what you want...

I tried on Vista having IE7 and XP having IE6, same effect, negative.

The problematic issue is: you can set text inside textbox using
InnerText, but you cannot get it using same property, again InnerText.
I was looking an explanation of it, not got so far.

If InnerText can set a value of an element, it should also get it, but
it cannot. When someone reads my first or summarized 2nd message with
absolute trial, anyone can experience what i mean.

regards,

ralpugan
 
Looks like IE specific (works in IE using JavaScript, doesn't work with FF).
Perhaps someone from MS thought it was making sense as the text is seen
"inside" the textbox so it is not really within the begin/end tags. Who
knows ?

Also, webbrowser control uses IE, couldn't get something about your
saying related to Firefox.
 
If InnerText can set a value of an element, it should also get it, but
it cannot. When someone reads my first or summarized 2nd message with
absolute trial, anyone can experience what i mean.

This is just an IE bug.

Actually having nothing when reading innerText is the correct behavior.
Being able to change the value by using innerText is a BUG as the "value"
attribute IS NOT innerText.

The layout for a tag is :

<MyTag value="ThisIsTheValueAttribute">ThisIsInnerText</MyTag>

So IMO your best bet is to stop wondering. Being able to change the value
attribute using InnerText should NOT work.

As what you want to is to get/set an attribute, just use
SetAttribute/GetAttribute and forget about using InnerText...
 
Also, webbrowser control uses IE, couldn't get something about your
saying related to Firefox.

I meant precisely that the bug you see is because the web browser control
uses IE under the hood.

I have been able to test this behavior with just an HTML + JavaScript page
and got the behavior you described in IE (and you have the same in the
webbrowser control as it uses IE) and the correct behavior in FF.
 
I meant precisely that the bug you see is because the web browser control
uses IE under the hood.

I have been able to test this behavior with just an HTML + JavaScript page
and got the behavior you described in IE (and you have the same in the
webbrowser control as it uses IE) and the correct behavior in FF.

Thanks Patrice for your replies, but that brings another bug or
confusing question. Based on your sample HTML code, using InnerText
property should output as "ThisIsInnerText". Right? But when i use the
code below InnerText property still returns nothing. Try that code in
a blank html with webbrowser control (inside body of course) to get
"ThisIsInnerText" with no luck:

' Assuming there's only one element tagged as "MyTag"
MsgBox(WebBrowser1.Document.GetElementsByTagName("MyTag").Item
(0).InnerText.ToString)

...no way. Why?

regards,

ralpugan
 
Sorry MyTag was a placeholder for a valid HTML tag. To test try for example
:

<html>
<body>
<span id="ctl" value="ValueAttribute">InnerText</span>
<input type="button"
onclick="alert(document.getElementById('ctl').innerText);alert(document.getElementById('ctl').getAttribute('value'));"
value="Get">
</body>
</html>

And open this web page in IE. When the Get button is pressed, it shows first
InnerText and then ValueAttribute. You'll likely have the same result using
the webbrowser control as the call is just handled by the IE engine under
the hood.
 
Sorry MyTag was a placeholder for a valid HTML tag. To test try for example
:

<html>
<body>
<span id="ctl" value="ValueAttribute">InnerText</span>
<input type="button"
onclick="alert(document.getElementById('ctl').innerText);alert(document..getElementById('ctl').getAttribute('value'));"
value="Get">
</body>
</html>

And open this web page in IE. When the Get button is pressed, it shows first
InnerText and then ValueAttribute. You'll likely have the same result using
the webbrowser control as the call is just handled by the IE engine under
the hood.

Thanks, so the conclusion: Using InnerText to set a text inside a
textbox element should NOT have worked though it works. And it's
proper to use SetAttribute/GetAttribute methods to get/set text in a
textbox. Right?

Also, based on your sample, trying to get InnerHtml of span tag
returns the" InnerText" string, so is that kind of bugs related to
IE6/7 behind the scenes?

Thanks for the clarification.

ralpugan
 
Thanks, so the conclusion: Using InnerText to set a text inside a
textbox element should NOT have worked though it works. And it's
proper to use SetAttribute/GetAttribute methods to get/set text in a
textbox. Right?

Correct. I strongly recommend this approach as using innerText for this is
not logical even if it works...
Also, based on your sample, trying to get InnerHtml of span tag
returns the" InnerText" string, so is that kind of bugs related to
IE6/7 behind the scenes?

This is the expected behavior so no bug IMO here (to see the difference add
another span within the span, innerText will return only the content,
innerHTML will include the markup).

Generally speaking, yes, as the webbrowser just direct the call to the IE
engine, if IE does something weird, you'll see the same strange behavior in
the webbrowser control...
 
Back
Top