Question Firing a sequence of events at a WebBrowser control

AutomationGuy

Member
Joined
Feb 17, 2010
Messages
5
Programming Experience
5-10
Hi folks

I'm doing some simple web-browser automation, the flow of which is these five steps...

  1. Open a test URL
  2. Enter a value into one text box
  3. Locate and store a reference to a html element (a submit button) which is buried in an iFrame on the page
  4. Enter User ID and Password in two text boxes and click a Login link
  5. Click the previously acquired submit button (I get an unauthorised access exception if I try to find the submit button after logging-in, hence why I grab it before logging in)

My code works fine, and looks a like this...

VB.NET:
'' JOB#1 - open the test url
wb.Navigate(testUrl)
'' custom handler for waiting for the page to fully load
WaitForPageLoad()

'' JOB#2 - enter a value into a text box
wb.Document.GetElementById(userReferenceId).SetAttribute("Value", someValue)

'' JOB#3 - locating the submit button on the iFrame, storing it in 'oSubmit'
For Each oElement As HtmlElement In wb.Document.Window.Frames(0).Document.All
  If Not IsNothing(oElement) Then
   Dim theId As String = oElement.Id
   If theId.Contains(submitButtonId) Then
    oSubmit = oElement
    Exit For
   End If
  End If
Next

'' JOB#4 - entering the user name, password and logging in
wb.Document.GetElementById("UserName").SetAttribute("Value", myUserName)
wb.Document.GetElementById("UserPassword").SetAttribute("Value", myUserPassword)
wb.Document.GetElementById("SignIn").InvokeMember("click")
'' custom handler for waiting for the page to fully load
WaitForPageLoad()

'' JOB#5 - clicking the submit button after logging-in
oSubmit.InvokeMember("click")

The problem I've got is that when I run the code as you see it, it will work right-up until that very last button click, at which point it fails to click the button... I don't get an exception or any errors, it just doesn't click the button.

If I break the code up into five different subs (i.e. one for each 'JOB' as labelled in the code), then add 5 Buttons to my Windows Form and move the JOB#1 code to Button1, and the JOB#2 code to Button2 etc. If I then run the app, and click Button 1, then click Button 2, then Button 3, then Button 4... when I click Button 5 (i.e. the one line of code that will click the oSubmit button), it works perfectly.

I can't work-out what the difference is between running all those lines of code in one method (which doesn't work), versus running them separately via buttons (which does work).

... the obvious answer would be that it's some sort of timing problem, and the last button isn't ready to be clicked, but even if I insert a 30 second delay before that last click, it still doesn't work.

I'm sure this is something really daft, but I just can't see the solution...

Any suggestions anyone?
 
Back
Top