-1
 private void Form1_Load(object sender, EventArgs e)
{
  webbrowser.Navigate(url);
}
 private async void buttonDownload_Click(object sender, EventArgs e)
 {
      await Task.Run(()=> {
                MessageBox.Show(webBrowser.Document.GetElementsByTagName("body")[0].InnerHtml);
            });
}

The error displayed "The specified conversion is not valid" i read about it and it something because is another thread not ui thread, do you know a simple and short way to make this work ?

JuulFan
  • 21
  • 7
  • 3
    Remove the Task. When the Document is completed (i.e., when `DocumentCompleted` is raised and `webBrowser.ReadyState == WebBrowserReadyState.Complete`), you can just read `webBrowser.Document.Body.InnerHtml`. Running a Task is really not what you want, plus, all content is already there. But, read the notes here: [How to get an HtmlElement value inside Frames/IFrames?](https://stackoverflow.com/a/53218064/7444103) – Jimi Jul 05 '20 at 14:21
  • Not directly related to the question, but you might find this helpful: [WebBrowser wait until page has been fully loaded](https://stackoverflow.com/questions/59473425/system-windows-forms-webbrowser-wait-until-page-has-been-fully-loaded/59474276#59474276) – Theodor Zoulias Jul 05 '20 at 16:30
  • Basically i invoke a click in a button when document is completed, then the action of the button will display new html code through ajax and i want to get that response and this response can't be handle by DocumentCompleted because the page is never refreshing by ajax, so must be something like that using await.task.run but it doesnt work. – JuulFan Jul 06 '20 at 11:45

1 Answers1

0

Edit You must subscribe to webBrowser.DocumentCompleted event

webBrowser.DocumentCompleted += completed;

private void completed(object sender, WebBrowserDocumentCompletedEventArgs e)
{
      if (sender is WebBrowser w)
      {
           var a = w.Document.GetElementsByTagName("body")[0].InnerHtml;
      }
}

private async void buttonDownload_Click(object sender, EventArgs e){
    webBrowser.Navigate(url);
}

enter image description here

I would rewrite your download method like this (I assume the webBrowser object is doing some 'long' computation and you don't want to block the UI thread)

private async void buttonDownload_Click(object sender, EventArgs e)
 {
     var htmlString = await Task.Run(()=> {
               return webBrowser.Document.GetElementsByTagName("body")[0].InnerHtml;
            });

     MessageBox.Show(htmlString);
}

Also, are you sure that is the correct error? perhaps you overlooked some inner exception, which could explain more

  • Thanks for you answer, unfortunately this dont, work, same problem i have to add i have declared webBrowser like this. – JuulFan Jul 05 '20 at 13:49
  • public WebBrowser webBrowser = new WebBrowser(); – JuulFan Jul 05 '20 at 13:49
  • Ok, did some research. Try this: `WebBrowser w = new WebBrowser(); w.Navigate("www.google.com"); w.DocumentCompleted += (object sender, WebBrowserDocumentCompletedEventArgs e) => { var a = w.Document.GetElementsByTagName("body")[0].InnerHtml; };` – Johnny Andrew Jul 05 '20 at 14:02
  • Basically i invoke a click in a button when document is completed, then the action of the button will display new html code through ajax and i want to get that response and this response can't be handle by DocumentCompleted because the page is never refreshing by ajax, so must be something like that using await.task.run but it doesnt work. – JuulFan Jul 06 '20 at 11:45