fergustr
fergustr

Reputation: 31

Interacting with web dropdown, .dispatchEvent

I am working on a web crawler of sorts that pulls back prices of items from various websites. The issue I am having is that on amazon.com, on items which have multiple options (i.e. sizes), I am able to interact, and change the dropdown selection option, but I can't get the site to post-back and display the specific price for that option. I realize that this isn't an uncommon question, but most people seem to have their issue resolved by using .dispatchEvent method rather than .fireEvent. That doesn't seem to be working for me.

Dropdown options (only showing first 5):

            <option value="-1" id="native_size_name_-1" data-a-id="size_name_-1" selected=""> Select</option>    

            <option value="0,B001249LAS" class="dropdownAvailable" data-a-css-class="dropdownAvailable" id="native_size_name_0" data-a-id="size_name_0" data-a-html-content="44W x 29L">
                44W x 29L
            </option>

            <option value="1,B001249KAE" class="dropdownAvailable" data-a-css-class="dropdownAvailable" id="native_size_name_1" data-a-id="size_name_1" data-a-html-content="44W x 30L">
                44W x 30L
            </option>

            <option value="2,B001243XPM" class="dropdownAvailable" data-a-css-class="dropdownAvailable" id="native_size_name_2" data-a-id="size_name_2" data-a-html-content="44W x 32L">
                44W x 32L
            </option>

            <option value="3,B001243XRK" class="dropdownAvailable" data-a-css-class="dropdownAvailable" id="native_size_name_3" data-a-id="size_name_3" data-a-html-content="44W x 34L">
                44W x 34L
            </option>

            <option value="4,B001243Z00" class="dropdownAvailable" data-a-css-class="dropdownAvailable" id="native_size_name_4" data-a-id="size_name_4" data-a-html-content="46W x 29L">
                46W x 29L
            </option>


            <option value="5,B001245XG4" class="dropdownAvailable" data-a-css-class="dropdownAvailable" id="native_size_name_5" data-a-id="size_name_5" data-a-html-content="46W x 30L">
                46W x 30L
            </option>

    </select><span style="min-width: 0%;" tabindex="-1" id="dropdown_selected_size_name" data-a-class="aui-variation  a-fastclick-disable" class="a-button a-button-dropdown aui-variation  a-fastclick-disable"><span class="a-button-inner"><span class="a-button-text a-declarative" data-action="a-dropdown-button" aria-haspopup="true" role="button" tabindex="0"><span class="a-dropdown-prompt"> Select</span></span><i class="a-icon a-icon-dropdown"></i></span></span></span>

Firefox shows me that there is an event associated with with the dropdown:

"native_dropdown_selected_size_name").onchange = function() {
TwisterNonJs.handleDropDown[0]();

Here is my code, Excel seems to compile the .dispatchEvent method just fine, but IE does not fire when it is initiated.

URLPath = http://www.amazon.com/Lee-Big-Tall-Dungarees-Carpenter-Original/dp/B001243Z00/ elementid = "priceblock_ourprice" (With Quotes)

Sub getPrice(URLPath As String, elementid As String)

Set IE = CreateObject("internetexplorer.application")

IE.Visible = True
navigate:
IE.navigate URLPath


Do While IE.readystate <> 4: DoEvents: Loop
    Set doc = CreateObject("htmlfile")
    Set doc = IE.document

    If doc Is Nothing Then GoTo navigate
    doc.getElementById("native_dropdown_selected_size_name").Focus
            doc.getElementById("native_dropdown_selected_size_name").SelectedIndex = 2

    Dim objEvent
    Set objEvent = doc.createEvent("HTMLEvents")
    objEvent.initEvent "change", False, True
    doc.getElementById("native_dropdown_selected_size_name").dispatchEvent objEvent

    Do While IE.readystate <> 4: DoEvents: Loop

    pricestr = doc.getElementById(elementid).innertext

Upvotes: 2

Views: 723

Answers (1)

fergustr
fergustr

Reputation: 31

After many Google searches I found my answer. I had the True & False properties backwards in the .initEvent line. I had:

objEvent.initEvent "change", False, True

It needed to be:

objEvent.initEvent "change", True, False

Hope this helps someone in the future.

Upvotes: 1

Related Questions