0

I want to copy all visible text from WebBrowser.

Clipboard.SetText(WebBrowser1.Document.Body.InnerText)

This code is working, but it's also getting text between <div style="display:none"> and </div>, which i don't want (i want only text that is visible when i manually go to that website).

Ses Manijak
  • 133
  • 1
  • 2
  • 13

1 Answers1

1

This works for me against google.com. This is mostly a translation of the c# version of the same question mentioned in the comments above.

    Dim text As String
    WebBrowser1.Document.ExecCommand("SelectAll", False, Nothing)
    WebBrowser1.Document.ExecCommand("Copy", False, Nothing)
    text = Clipboard.GetText()
    MessageBox.Show(text, "Text")
Alexander Ryan Baggett
  • 2,347
  • 4
  • 34
  • 61
  • This idea works, thank you for that. But, any ideas how to do this without using clipboard? Thanks again. – Ses Manijak Feb 06 '17 at 08:51
  • Attempts to do this without the clipboard will require serious XML parsing. – Alexander Ryan Baggett Feb 06 '17 at 15:32
  • You can take a look at this post where they show how to check if an element is visible. http://stackoverflow.com/questions/14776840/htmlagilitypack-how-to-check-if-an-element-is-visible – Alexander Ryan Baggett Feb 06 '17 at 17:44
  • Once you know if it is visible, you could simply do a linq statement on the body like so : Dim VisibleText = From element In body.DescendantsAndSelf() Where CheckIfVisible(element) Select element.InnerText – Alexander Ryan Baggett Feb 06 '17 at 17:45
  • I added Clipboard.Clear() and duplicated second line in your code, now it works perfectly for me. Thank you very much Alexander! :) – Ses Manijak Feb 08 '17 at 16:09