424,303 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,303 IT Pros & Developers. It's quick & easy.

WebBrowser DocumentText getting problem ...

P: n/a
I have very strange and stupid problem with .NEt's webBrowser control...

If I do this:
----------------------------------
private void btnGoogle_click(object sender, EventArgs e)

{

webBrowser1.Navigate(http://www.google.com);

}
----------------------------------

and after that try this:

-----------------------------------

string html;

html = webBrowser1.DocumentText.ToString();

------------------------------------

I get error: "The system cannot find the file specified. (Exception from
HRESULT: 0x80070002)"
BUT
If I open Google (or any other page), and if I then click any link in
webBrowser, and after that try to get html content, all works fine...

Is there any way to get HTML content while navigating automaticly (with my
buttons)..

Hope I was clear :)

Thanks in advance.

Electronic.HR
Jun 5 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
The content is fetched in the web browser asynchronously. You should
wait until the downloading is done before you try and access the
DocumentText property. You can do this by subscribing to the
DocumentCompleted event and then getting the text from the document from
there.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"- Electronic.HR -" <el***********@gmail.comwrote in message
news:f4**********@news1.carnet.hr...
>I have very strange and stupid problem with .NEt's webBrowser control...

If I do this:
----------------------------------
private void btnGoogle_click(object sender, EventArgs e)

{

webBrowser1.Navigate(http://www.google.com);

}
----------------------------------

and after that try this:

-----------------------------------

string html;

html = webBrowser1.DocumentText.ToString();

------------------------------------

I get error: "The system cannot find the file specified. (Exception from
HRESULT: 0x80070002)"
BUT
If I open Google (or any other page), and if I then click any link in
webBrowser, and after that try to get html content, all works fine...

Is there any way to get HTML content while navigating automaticly (with my
buttons)..

Hope I was clear :)

Thanks in advance.

Electronic.HR
Jun 5 '07 #2

P: n/a
Hm...good idea.. but it doesn't work either...
I done this
<CODE>
private void webBrowser1_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{

string html;

MessageBox.Show("Document Completed");
html = webBrowser1.DocumentText.ToString();
MessageBox.Show(html);

}
</CODE>
first message box appears, but second one doesn't!
(In this case nothing happens. In this case there's no Exception showing,
but second message box doesn't appear) ?!?
Jun 5 '07 #3

P: n/a
Well, by default, exceptions thrown by event handlers on controls are
swallowed. You will have to wrap in a try/catch block to figure out what
the exception is (you can look at it in the debugger too).

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"- Electronic.HR -" <el***********@gmail.comwrote in message
news:f4**********@news1.carnet.hr...
Hm...good idea.. but it doesn't work either...
I done this
<CODE>
private void webBrowser1_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{

string html;

MessageBox.Show("Document Completed");
html = webBrowser1.DocumentText.ToString();
MessageBox.Show(html);

}
</CODE>
first message box appears, but second one doesn't!
(In this case nothing happens. In this case there's no Exception showing,
but second message box doesn't appear) ?!?
Jun 5 '07 #4

P: n/a
When I click on a link in webBrowser, only then DocumentText fills with
content...

What's difference between:
- clicking on a link in webBrowser and
- calling webBrowser1.Navigate(...);

?
Jun 5 '07 #5

P: n/a
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2****************@TK2MSFTNGP04.phx.gbl...
Well, by default, exceptions thrown by event handlers on controls are
swallowed. You will have to wrap in a try/catch block to figure out what
the exception is (you can look at it in the debugger too).
Try/catch gives no result....no exception..code just continues...
....
Now, when I call webBrowser1.Navigate(...), and watch DocumentText in
debugger after DocumentCompleted event this is what I get:

<DOCUMENT_TEXT>
- DocumentText '((System.Windows.Forms.WebBrowser)(sender)).Docum entText'
threw an exception of type 'System.IO.FileNotFoundException' string
{System.IO.FileNotFoundException}
- base {"The system cannot find the file specified. (Exception from HRESULT:
0x80070002)":null} System.IO.IOException {System.IO.FileNotFoundException}
- FileName null string
- FusionLog null string
- Message "The system cannot find the file specified. (Exception from
HRESULT: 0x80070002)" string

</DOCUMENT_TEXT>

but.. when I click on a link in webBrowser and then look at DocumentText
after DocumentCompleted event i get this:
- DocumentText -it's value is HTML content of a page, and type is
string...all is prefect :-(
I really don't have idea why is this happening...

p.s. Thanks for helping, Nicholas :)


Jun 5 '07 #6

P: n/a
Im only suggesting wrapping the first call in the event handler to the
DocumentText property, not the whole thing. Regardless, if that second line
was not executing, then an exception could be the onlyh reason.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"- Electronic.HR -" <el***********@gmail.comwrote in message
news:f4**********@news1.carnet.hr...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:%2****************@TK2MSFTNGP04.phx.gbl...
> Well, by default, exceptions thrown by event handlers on controls are
swallowed. You will have to wrap in a try/catch block to figure out what
the exception is (you can look at it in the debugger too).

Try/catch gives no result....no exception..code just continues...
...
Now, when I call webBrowser1.Navigate(...), and watch DocumentText in
debugger after DocumentCompleted event this is what I get:

<DOCUMENT_TEXT>
- DocumentText '((System.Windows.Forms.WebBrowser)(sender)).Docum entText'
threw an exception of type 'System.IO.FileNotFoundException' string
{System.IO.FileNotFoundException}
- base {"The system cannot find the file specified. (Exception from
HRESULT: 0x80070002)":null} System.IO.IOException
{System.IO.FileNotFoundException}
- FileName null string
- FusionLog null string
- Message "The system cannot find the file specified. (Exception from
HRESULT: 0x80070002)" string

</DOCUMENT_TEXT>

but.. when I click on a link in webBrowser and then look at DocumentText
after DocumentCompleted event i get this:
- DocumentText -it's value is HTML content of a page, and type is
string...all is prefect :-(
I really don't have idea why is this happening...

p.s. Thanks for helping, Nicholas :)

Jun 5 '07 #7

P: n/a
Hm.. I can't believe.. LOL...

Problem is that webBrowser control MUST be FOCUSED before taking
DocumentText... :)
When I click on a link in browser it get's automaticly focused, that's why I
always got DocumentText when I clicked link..
but When I call navigate, webbrowser doesn't get focused, so i have to focus
it manually before taking DocumentText...

Anyway, thanks a lot for helping! ;)

BR

Jun 5 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.