Link to home
Start Free TrialLog in
Avatar of bswiftly
bswiftly

asked on

Web Browser Control (AxSHDocVw) & sendkeys.. won't work!

What I need to do is to log into a website, and download a file.  

In order to do that I need to use this AxSHDocVw control and sendkeys to tab and enter the username / password, click the download link and save it.

(downloading would be a secondary question)  For now I need to figure out how to do the sendkeys.  

One of 2 things is happening.

1) the page doesn't have focus, so the sendkeys doesn't happen
2) the sendkeys happens before the page is loaded.. (i tried a thread.sleep, even after the navigate method, but somehow this prevents it from loading until the sleeper is over.. see below!)

     ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf doNavigate), url)
     Thread.Sleep(10000)
     SendKeys.Send("{TAB}")
etc etc etc
OR
      browser.Navigate(url)
     Thread.Sleep(10000)
     SendKeys.Send("{TAB}")
etc etc etc

I also tried it without the sleeper.

I also tried it using the navigate2complete event.

private sub browser_NavigateComplete2(ByVal sender As Object, ByVal e As browser.DWebBrowserEvents2_NavigateComplete2Event) Handles browser.NavigateComplete2

and put the above code int he method.  I'm lost!


nothing seems to work..  
I can use:
System.Diagnostics.Process.Start(url)    but if i use that I have no way of closing the browser  (intelligently) after the sendkeys stuff has done its thing on the web browser (this DOES work, because the sleep function doesn't prevent the browser from loading.)

HELP!
Avatar of testn
testn

you should hook up to DocumentComplete event instead
Avatar of bswiftly

ASKER

how would i do that?
no documentComplete event that I can find....
k that works sometimes but not really. half the time the documentcomplete event fires before the document is actually loaded and visible and able to take keystrokes.


still stuck.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is how I download image data from the URL:

    Dim clientReader As New WebClient

    Dim bufferImageData As Byte() = clientReader.DownloadData(imageURL)

    Dim streamImage As New MemoryStream(bufferImageData)

    Return New Bitmap(streamImage)


Bob
thanks bob.. i have a solution that works but that might help things out a little.

there is one thing i need that isn't working so far.. and that is a save file dialog.  

How would I go about saving the file from that to a location easily?

sendkeys won't work because i can't find an event for when the dialog pops up..

and there isn't a straight link to the file for download!
the save file dialog pops up from a javascript submit function, that is called on the form submit button it seems.   so i need to click the button on the form and then download the file that is prompted.    
I wish I could do it your way, without sendkeys, but there are some things I can't do, like selecting listbox elements, etc... without sendkeys...  especially this Save File Dialog, and file browser (have to tell it the exact location to save too!)


I don't know of any way, yet.

Bob
http://support.microsoft.com/default.aspx?scid=kb;en-us;325204

I have worked through the file download via sendkeys.  Although I'm trying to work through the above so that I know when the file is downloaded etc. and can close the browser when it is..

I am having troubles with this as well.

Should this intercept the filedownload?   That is what I would prefer to have happen, intercept it and save the file automatically, in a specific location.

That article states that this is a problem only in C#, and if you are using VB.NET it might not be a problem.  Is the WebBrowser_FileDownload event not firing?

Bob
not for me...  even after the save button is clicked that event still doesn't fire.

I've tried both adding a custom method and attaching an event to it that way, and also in vs.net going to the dropdown list and selecting teh event..  both should work the same and I don't see the event being fired in either case.