Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27017

VS 2010 Copy text from webpage in (unseen) web browser - help please?

$
0
0
I'm looking to load a simple Google search query into a web browser that is not displayed on screen, and then get the text of the page (similar to Ctrl-A, Ctrl-C from the keyboard) to work with. With the URL
I can pull that up in an actual browser, type Ctrl-A then Ctrl-C, and this is what I get in Notepad:
Quote:

Screen reader users, click here to turn off Google Instant.
WebImagesMapsShoppingNewsMoreSearch tools
About 130,000,000 results (0.21 seconds)
Search Results
New York, NY
Friday 3:00 PM
Haze
undefined
97°F | °C
Precipitation: 10%
Humidity: 45%
Wind: 8 mph
TemperaturePrecipitationWind
...
That's what I would like to get on the clipboard with the program. What I have so far has come from searching on this forum & MSDN & a couple other places, and I've reached the point of "stuck." I'm sure it's probably a couple of simple mistakes, but right now the following code puts nothing at all on the clipboard, plus a couple of other minor (?) glitches noted in the comments below:
Code:

Private Sub btnGrabUrl_Click(sender As System.Object, e As System.EventArgs) Handles btnGrabUrl.Click

        Clipboard.Clear()

        If tboxUrlToGrab.Text.Length = 0 Then
            tboxUrlToGrab.Text = "http://www.google.com/search?q=weather+NYC"          ' default unless I type something else in the "input" textbox
        End If

        Dim UrlToGrab As String = tboxUrlToGrab.Text

        Dim wb As New WebBrowser
        wb.Navigate(UrlToGrab)

        Dim Timeout As Date = Now.AddSeconds(10)
        Do
            System.Threading.Thread.Sleep(1000)
        Loop Until wb.ReadyState = WebBrowserReadyState.Loaded Or Now > Timeout  ' always times out.

        If Not wb.ReadyState = WebBrowserReadyState.Loaded Then
            MsgBox("Step 1 timed out waiting to load the URL.")    ' always happens, although the page DOES in fact load, quickly
        End If
        MsgBox("Step 1 URL: >" & wb.Url.ToString & "<")          ' shows the actual URL

        tboxReturnedWebPage.Text = "Step 1 Document Text: >" & wb.DocumentText & "<"              ' shows Google's HTML code (LONG!)

        MsgBox("About to try step 2...")

        wb.Document.ExecCommand("SelectAll", False, vbNull)      ' seems this *should* select all text in the document, as Ctrl-A

        System.Threading.Thread.Sleep(1000)

        wb.Document.ExecCommand("Copy", False, vbNull)      ' seems this *should* copy to the clipboard, as Ctrl-C

        System.Threading.Thread.Sleep(1000)

        tboxReturnedWebPage.Text = Clipboard.GetText      ' the textbox goes blank shortly after clicking OK to the "step 2" msgbox

    End Sub

If anybody can spot what I'm doing wrong here, I'd be most appreciative!

Viewing all articles
Browse latest Browse all 27017

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>