与网站下拉菜单互动,.dispatchEvent

我正在从事各种网站的物品价格的networking爬虫工作。 我遇到的问题是,在amazon.com上,有多个选项(即大小)的项目,我可以交互,并更改下拉select选项,但我不能让网站回传和显示该选项的具体价格。 我意识到这不是一个不常见的问题,但大多数人似乎通过使用.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 

经过多次Googlesearch,我find了我的答案。 我在.initEvent行中有了True&False属性。 我有:

 objEvent.initEvent "change", False, True 

它需要是:

 objEvent.initEvent "change", True, False 

希望这有助于未来的人。