PDA

View Full Version : Solved: Controlling a webpage dropdown



bolekblues
06-25-2012, 11:58 AM
Hi,

First of all, glad to be a member of the forum. I hope to contribute by helping others solve their cases and to get some valuable knowledge.

I have some trouble in navigating a webpage from vba. I have done similar things in the past, but they were rather simpler.

On the orange 'carhire' tab I would like to fill the input boxes / drop down list with variables and then click 'search'. The first two (pickup country and airport) seem to be a cascade lists - airport depends on the country you choose and in some cases there are more than two dependant fields after the country (town/cities and location), like for UK.

Here is the code I have tried. It seems to work for some fields, but not for all. WEBPAGE is 'travelsupermarket <dot> com'

Thank you for your help


Sub TS1()

Dim ie As Object
Set ie = CreateObject("internetexplorer.application")

ie.Visible = True
ie.navigate WEBPAGE

While ie.busy
DoEvents
Wend

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

ie.document.getElementById("container_ucCarHireSHPGadget_ddlCountry").Value = "USA"
'pickupCountry ////does not work
ie.document.getElementById("container_ucCarHireSHPGadget_calPickUpDate").Value = "01/08/2012"
'pickUpDate
ie.document.getElementById("container_ucCarHireSHPGadget_calDropOffDate").Value = "05/08/2012"
'dropOffDate
ie.document.getElementById("container_ucCarHireSHPGadget_ddlCarType").Value = "Economy"
'carType ////does not work
ie.document.getElementById("container_ucCarHireSHPGadget_ddlDriverAge").Value = "30"
'age

End Sub

bolekblues
06-27-2012, 12:54 AM
Anyone please??

Here is the code that (I think) is responsible for the first dropdown list.



<fieldset class="">
<div id="container_ucCarHireSHPGadget_deciPickUpCountry" class="msfg-travel-carhire-decilocations">
<label for="container_ucCarHireSHPGadget_ddlCountry">Pick up country</label><select name="container$ucCarHireSHPGadget$ddlCountry" id="container_ucCarHireSHPGadget_ddlCountry" class="msfg-travel-carhire-locationdropdowns" onchange="SetUKSpecificSettings();">

</select><span id="container_ucCarHireSHPGadget_ValRequiredDropDownListForDdlCountry" style="color:Red;display:none;"></span>&nbsp;<em>*</em>
</div>
</fieldset>

<fieldset class="">
<div id="container_ucCarHireSHPGadget_deciArea" class="msfg-travel-carhire-decilocations">
<label for="container_ucCarHireSHPGadget_ddlArea">Airport</label><select name="container$ucCarHireSHPGadget$ddlArea" id="container_ucCarHireSHPGadget_ddlArea">

</select><span id="container_ucCarHireSHPGadget_ValRequiredDropDownListForDdlArea" style="color:Red;display:none;"></span>&nbsp;<em>*</em>
</div>
</fieldset>

<div id="container_ucCarHireSHPGadget_locationContainer" style="display:none;">
<fieldset class="">
<div id="container_ucCarHireSHPGadget_deciAirport" class="msfg-travel-carhire-decilocations">
<label for="container_ucCarHireSHPGadget_ddlLocation">Location</label><select name="container$ucCarHireSHPGadget$ddlLocation" id="container_ucCarHireSHPGadget_ddlLocation" class="msfg-travel-carhire-locationdropdowns">

</select><span id="container_ucCarHireSHPGadget_ValRequiredDropDownListForDdlLocation" style="color:Red;display:none;"></span>&nbsp;<em>*</em>
</div>
</fieldset>
</div>

MrMom
06-27-2012, 11:23 AM
I will be watching for a resolve on your issue closely as I too have a very similar problem. I am trying to capture data after a query from a dropdown box with index's for filtering.
Good luck.

bolekblues
06-28-2012, 02:20 AM
The below seems to work but onchange still does not trigger. I need only this dropdown, the rest is working.

ie.document.getElementById("container_ucCarHireSHPGadget_ddlCountry").Value = 32
ie.document.getElementById("container_ucCarHireSHPGadget_ddlCountry").onchange

Anyone please?

bolekblues
07-03-2012, 01:36 AM
Can anyone please assist? I need just sth like onchange to trigger the next dropdown list.

JP2112
07-03-2012, 06:05 AM
I'm afraid you haven't explained what is wrong. Where are you stuck?

bolekblues
07-03-2012, 06:30 AM
If you enter the site, you can see an orange box in the middle. You choose 'car Hire' tab and then I would like the VBA to populate all values and click search. I can do it for all of the boxes apart from dropdowns: 'pickup country' and 'airport', the latter seems to be triggered by the latter. This is where i got stuck... I can set a value for 'pickup country', but this does not trigger the second dropdown to show relevant values; ie when a user chooses USA, it should show only USA airports...

Thank you
bolekblues

stanl
07-03-2012, 06:30 AM
The below seems to work but onchange still does not trigger. I need only this dropdown, the rest is working.

ie.document.getElementById("container_ucCarHireSHPGadget_ddlCountry").Value = 32
ie.document.getElementById("container_ucCarHireSHPGadget_ddlCountry").onchange

Anyone please?

try

ie.document.getElementById("container_ucCarHireSHPGadget_ddlCountry").fireevent("onchange")

bolekblues
07-03-2012, 06:54 AM
yeah i have tried fireevent as well... still does not recognize the value and does not trigger the second dropdown

stanl
07-03-2012, 11:55 AM
yeah i have tried fireevent as well... still does not recognize the value and does not trigger the second dropdown

Yeah, I forgot to mention the important part

ie.document.getElementById("container_ucCarHireSHPGadget_ddlCountry").Value = 32

for dropdowns use

ie.document.getElementById("container_ucCarHireSHPGadget_ddlCountry").SelectedIndex = 32

bolekblues
07-03-2012, 01:01 PM
I have used the below... still not working...

ie.document.getElementById("container_ucCarHireSHPGadget_ddlCountry").SelectedIndex = 32
'ie.document.getElementById("container_ucCarHireSHPGadget_ddlCountry").Value = 32
'ie.document.getElementById("container_ucCarHireSHPGadget_ddlCountry").onchange
ie.document.getElementById("container_ucCarHireSHPGadget_ddlCountry").fireevent ("onchange")

stanl
07-04-2012, 02:56 AM
Just use

ie.document.getElementById("container_ucCarHireSHPGadget_ddlCountry").SelectedIndex = 32
ie.document.getElementById("container_ucCarHireSHPGadget_ddlCountry").fireevent ("onchange")

I'm assuming 32 is the 32nd country in the dropdown... you are using getElementByID. I generally use something like

oIE.Document.Body.GetElementsByTagName("SELECT").Item(8).SelectedIndex =4

but you never know.:dunno

bolekblues
07-04-2012, 03:51 AM
Yes, I have tried what you suggested. The middle two lines are commented (i also tried them in a different scheme, all of them not working)

stanl
07-04-2012, 12:34 PM
Yes, I have tried what you suggested. The middle two lines are commented (i also tried them in a different scheme, all of them not working)

Well, without you providing a URL to test against all I can say is I have code similar to what I suggested working in a variety of apps against combo boxes in several applications.

bolekblues
07-04-2012, 01:54 PM
I have provided a link in the first post. It is /http://www.travelsupermarket.com/. (http://www.travelsupermarket.com/)

I know that i have to first click the orange car hire tab, but still cannot go any further...

As it stands, i have tried sth like this, as someone helped me with a different approach.



Sub TS3()

Dim IE As Object
Dim doc As HTMLDocument
Dim sel As HTMLSelectElement
Dim link As HTMLAnchorElement

Set IE = CreateObject("InternetExplorer.Application")

With IE
.Visible = True
.navigate "http://www.travelsupermarket.com/"
While .Busy Or .readyState <> 4: DoEvents: Wend
Set doc = .document
End With

With doc

'Click orange 'Car hire' tab

Set link = .getElementById("container_lnkTab3")
link.Click
Application.Wait DateAdd("s", 10, Now)

'Select Country = USA and trigger Airport dropdown

Set sel = .getElementById("container_ucCarHireSHPGadget_ddlCountry")
If Not sel Is Nothing Then
sel.Value = 32
sel.FireEvent "onchange"
Application.Wait DateAdd("s", 10, Now)
End If

'Select Airport (area) = Akron-Canton (and trigger next dropdown which would be displayed if country = UK)

Set sel = .getElementById("container_ucCarHireSHPGadget_ddlArea")
If Not sel Is Nothing Then
sel.Value = "CAK"
sel.FireEvent "onchange"
Application.Wait DateAdd("s", 3, Now)
End If

'Set pick up and drop off dates, driver age and car type

.getElementById("container_ucCarHireSHPGadget_calPickUpDate").Value = Format(Date + 1, "dd/mm/yyyy")
.getElementById("container_ucCarHireSHPGadget_calDropOffDate").Value = Format(Date + 6, "dd/mm/yyyy")
.getElementById("container_ucCarHireSHPGadget_ddlCarType").Value = "0"
.getElementById("container_ucCarHireSHPGadget_ddlDriverAge").Value = "30"

'Click Search - Results page is displayed after a few seconds

'.getElementById("container_ucCarHireSHPGadget_btnSearch").Click

End With

End Sub

stanl
07-05-2012, 07:53 AM
Sorry, I missed the URL in your initial post. Site uses iFrames, so parsing involves a couple more steps. I'll try to look it over and offer a solution, but right now work calls.

stanl
07-05-2012, 09:19 AM
OK, once I got a moment to look the site over - I went directly to the sub-nav:

cURL="http://www.travelsupermarket.com/c/cheap-car-hire"

Opened IE and navigated, then called

oIE.Document.Body.GetElementsByTagName("SELECT").Item(0).SelectedIndex=4
oIE.Document.Body.GetElementsByTagName("SELECT").Item(0).FireEvent("onchange")

which pulled up USA. The tricky part is that once 'fired' the airport drop down is re-configured for US airports, so you need to decide what airport then set the selectedindex to looking up the array element in the SELECT that corresponds to what you are after.

stanl
07-05-2012, 11:27 AM
Oh... as a final gotcha... if using IE9 you have to run in compatibility mode... not sure how to set that from vba :dunno

bolekblues
07-06-2012, 02:04 PM
Thank you very much for this. Actually this was a browser issue which caught me by a huge suprise. I downgraded from IE9 to IE8 and tweak the code slightly so that it selected the 'car hire' tab first and it worked.

stanl
07-07-2012, 12:29 PM
Thank you very much for this. Actually this was a browser issue which caught me by a huge suprise. I downgraded from IE9 to IE8 and tweak the code slightly so that it selected the 'car hire' tab first and it worked.

Glad it worked out. If you use IE9 the document.documentMode for the URL is 7 [IE7]. If using IE8 or less you have to use the document.Compatmode property

Be nice to be able to detect a sites document mode then adjust your browser object to effectively scrape data... which I wrote on another forum represents a 'Holy Grail'.

With sites like the one you referenced which have variable dropdown values based on the previous selection I generally write pre-code that iterates through all available 'options' per dropdown capturing the values in a fabricated recordset (either normal or shaped) which I either save as an xml lookup or move to db table lookups.My final code uses the lookups to select all possible indexes, then I traverse the site with those variables - makes for generic/reusable code.