Consulting

Results 1 to 20 of 20

Thread: Solved: Controlling a webpage dropdown

  1. #1

    Solved: Controlling a webpage dropdown

    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

    [vba]
    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").Va lue = "01/08/2012"
    'pickUpDate
    ie.document.getElementById("container_ucCarHireSHPGadget_calDropOffDate").V alue = "05/08/2012"
    'dropOffDate
    ie.document.getElementById("container_ucCarHireSHPGadget_ddlCarType").Value = "Economy"
    'carType ////does not work
    ie.document.getElementById("container_ucCarHireSHPGadget_ddlDriverAge").Val ue = "30"
    'age

    End Sub
    [/vba]

  2. #2
    Anyone please??

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

    HTML Code:
    <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>

  3. #3
    VBAX Newbie MrMom's Avatar
    Joined
    Jun 2012
    Location
    Memphis, TN
    Posts
    1
    Location
    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.

  4. #4
    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").oncha nge

    Anyone please?

  5. #5
    Can anyone please assist? I need just sth like onchange to trigger the next dropdown list.

  6. #6
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    I'm afraid you haven't explained what is wrong. Where are you stuck?
    Regards,
    JP

    Read the FAQ
    Getting free help on the web
    My website
    Please use [vba][/vba] tags when posting code

  7. #7
    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

  8. #8
    VBAX Master stanl's Avatar
    Joined
    Jan 2005
    Posts
    1,141
    Location
    Quote Originally Posted by bolekblues
    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").oncha nge

    Anyone please?
    try

    ie.document.getElementById("container_ucCarHireSHPGadget_ddlCountry").firee vent("onchange")

  9. #9
    yeah i have tried fireevent as well... still does not recognize the value and does not trigger the second dropdown

  10. #10
    VBAX Master stanl's Avatar
    Joined
    Jan 2005
    Posts
    1,141
    Location
    Quote Originally Posted by bolekblues
    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").Selec tedIndex = 32

  11. #11
    I have used the below... still not working...

    ie.document.getElementById("container_ucCarHireSHPGadget_ddlCountry").Selec tedIndex = 32
    'ie.document.getElementById("container_ucCarHireSHPGadget_ddlCountry").Valu e = 32
    'ie.document.getElementById("container_ucCarHireSHPGadget_ddlCountry").onch ange
    ie.document.getElementById("container_ucCarHireSHPGadget_ddlCountry").firee vent ("onchange")

  12. #12
    VBAX Master stanl's Avatar
    Joined
    Jan 2005
    Posts
    1,141
    Location
    Just use

    ie.document.getElementById("container_ucCarHireSHPGadget_ddlCountry").Selec tedIndex = 32
    ie.document.getElementById("container_ucCarHireSHPGadget_ddlCountry").firee vent ("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.

  13. #13
    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)

  14. #14
    VBAX Master stanl's Avatar
    Joined
    Jan 2005
    Posts
    1,141
    Location
    Quote Originally Posted by bolekblues
    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.

  15. #15
    I have provided a link in the first post. It is /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.

    [vba]

    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

    [/vba]

  16. #16
    VBAX Master stanl's Avatar
    Joined
    Jan 2005
    Posts
    1,141
    Location
    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.

  17. #17
    VBAX Master stanl's Avatar
    Joined
    Jan 2005
    Posts
    1,141
    Location
    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("onchang e")

    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.

  18. #18
    VBAX Master stanl's Avatar
    Joined
    Jan 2005
    Posts
    1,141
    Location
    Oh... as a final gotcha... if using IE9 you have to run in compatibility mode... not sure how to set that from vba

  19. #19
    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.

  20. #20
    VBAX Master stanl's Avatar
    Joined
    Jan 2005
    Posts
    1,141
    Location
    Quote Originally Posted by bolekblues
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •