Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2160
  • Last Modified:

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!
0
bswiftly
Asked:
bswiftly
  • 7
  • 4
1 Solution
 
testnCommented:
you should hook up to DocumentComplete event instead
0
 
bswiftlyAuthor Commented:
how would i do that?
0
 
bswiftlyAuthor Commented:
no documentComplete event that I can find....
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
bswiftlyAuthor Commented:
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.
0
 
Bob LearnedCommented:
I use the MSHTML library, and the DocumentComplete event, and go through the HTML looking for the right control, and use the appropriate methods to accomplish this task:

      Dim doc As HTMLDocumentClass = Me.browser.Document

      Dim elem As HTMLInputElementClass = doc.getElementById("SimpleSearchText")
      elem.focus()
      elem.value = StripChars(title)

      Dim button As HTMLInputElementClass = doc.getElementById("BasicGo")
      button.click()

Bob
0
 
Bob LearnedCommented:
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
0
 
bswiftlyAuthor Commented:
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!
0
 
bswiftlyAuthor Commented:
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!)


0
 
Bob LearnedCommented:
I don't know of any way, yet.

Bob
0
 
bswiftlyAuthor Commented:
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.

0
 
Bob LearnedCommented:
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
0
 
bswiftlyAuthor Commented:
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.

0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now