与网络下拉列表互动,.dispatchEvent

时间:2015-11-10 18:16:04

标签: excel vba excel-vba

我正在开发各种网络爬虫,可以从各种网站中提取物品的价格。我遇到的问题是,在amazon.com上,对于有多个选项(即大小)的项目,我能够进行交互,并更改下拉选择选项,但我无法让网站进行回发和显示该选项的具体价格。我意识到这不是一个不常见的问题,但大多数人似乎通过使用.dispatchEvent方法而不是.fireEvent解决了他们的问题。这似乎对我不起作用。

下拉选项(仅显示前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向我显示与下拉列表相关的事件:

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

这是我的代码,Excel似乎编译.dispatchEvent方法就好了,但IE在启动时不会触发。

URLPath = http://www.amazon.com/Lee-Big-Tall-Dungarees-Carpenter-Original/dp/B001243Z00/ elementid =“priceblock_ourprice”(带引号)

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

0 个答案:

没有答案