PDA

View Full Version : VBA: Problem clicking button on second page of a website form



lacemagic
12-15-2015, 09:05 AM
I am trying to automate the filling of a form, clicking submit, and then after that, clicking another button on the second page. My code so far successfully fills out the form and clicks the button loading the second page. However, on the second page I cannot get the button to click. When stepping through the code it clicks the second page button so I have properly identified the button but it is like the webpage is no longer selected when the code is running. Any help?

The Button on the second webpage page:


<tr><th colspan='3' style='padding-top:23px;padding-bottom:10px'><input type='button' value='Continue' class='v14b' onclick='if(verFrm())document.form1.submit();'></tr>

My code to open the first webpage, fill out necessary forms, and click the button. Then to click the button on the next page.



Sub FillICEForm()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")


ie.Navigate "website"


ie.Visible = True


While ie.busy
DoEvents
Wend


Application.Wait (Now + TimeValue("0:00:02"))


ie.document.ALL("sf").Value = "1"
ie.document.ALL("sd").Value = "100"
ie.document.ALL("cd").Value = "100" 'sd/sf
ie.document.ALL("ci").Value = "100"
ie.document.ALL("r").Value = "100"
ie.document.ALL("st").Value = "2" 'FROM SHEET

'Submit first form


Set the_input_elements = ie.document.getElementsByTagName("input")
For Each input_element In the_input_elements
If input_element.getattribute("class") = "v14b" Then
input_element.Click
Exit For
End If
Next input_element

'Second page


Set the_input_elements = ie.document.getElementsByTagName("input")
For Each input_element In the_input_elements
If input_element.getattribute("value") = "Continue" Then
input_element.Click
Exit For
End If
Next input_element






End Sub

lacemagic
12-15-2015, 11:07 AM
I have found one thing that is different stepping through vs. when it runs through.

When running it all the way through: After submitting the first form and loading the second page, if I look at the locals window for the IE object, it shows the old URL of the first page still even though the second page is loaded.
When stepping through with F8: After submitting the first form and loading the second page, if I look at the locals window for the IE object it shows the new URL of the second page.

Any reason for this difference? Why is it updating when stepping through but not when I run it?

SamT
12-15-2015, 12:18 PM
Why is it updating when stepping through but not when I run it?
Timing.
After the "Next" set some code to wait for IE to finish. One or two seconds would probably work, although I think that IE can return some value when it's ready for the next action.

lacemagic
12-15-2015, 12:53 PM
I wish it were so easy! Tried this but it does not help regardless of wait time.

lacemagic
12-15-2015, 01:22 PM
So I played around with adding even more waits than just after the next. I added weights all over the place and it works but only under one condition which is a useless one. When IE opens I have to click back into the VBA screen. If I don't it does not work as usual. I thought maybe then it would work if I just did not show the IE screen, but the same thing happens there. If I do not click in the VBA project are before the second page code runs it does not work. This is problematic because I plan on running this through multiple sets of inputs and need it to just go and not need me to click the VBA screen all the time. In any case it seems like a weird problem..