通过VBA更改Amazon ComboBox

我尝试编写一个VBAmacros,它使我的工作在Internet Explorer中自动化。 我的目标是赶上已经打开的亚马逊网页,并为“书籍”下拉菜单的价值。 目前我能够赶上运行亚马逊网站,但我不知道如何改变下拉菜单的价值。

Sub amazonCheck() Dim element As Object Dim i As Integer 'get opened Amazon session Dim shellWins As ShellWindows Dim IE As InternetExplorer Set shellWins = New ShellWindows i = shellWins.Count On Error Resume Next For i = 0 To i If shellWins.Item(i).Parent = "Internet Explorer" Then Debug.Print shellWins.Item(i).Document.URL shellWins.Item(i).Visible = True If shellWins.Item(i).Document.URL = "http://www.amazon.com/" Then Set IE = shellWins.Item(i) Exit For End If End If Next i On Error GoTo 0 'try to get dropdown box/ I think it is working fine, Set element = IE.Document.getElementByID("searchDropdownBox").Item(0) 'try to change a value of dropdown box/ It does nothing element.Value = "Books" End Sub 

在这里你可以看到一个亚马逊HTML代码的例子(我不知道HTML是可能的,我在代码的错误部分search)

 <select class="nav-search-dropdown searchSelect" data-nav-digest="zOQ511tmF9BlHuHLk7aFmmjL+iA" data-nav-selected="0" id="searchDropdownBox" name="url" tabindex="18" title="Search in"> <option selected="selected" value="search-alias=aps">All Departments</option> <option value="search-alias=instant-video">Amazon Video</option> <option value="search-alias=appliances">Appliances</option> <option value="search-alias=mobile-apps">Apps &amp; Games</option> <option value="search-alias=arts-crafts">Arts, Crafts &amp; Sewing</option> <option value="search-alias=automotive">Automotive</option> <option value="search-alias=baby-products">Baby</option> <option value="search-alias=beauty">Beauty</option> <option value="search-alias=stripbooks">Books</option> <option value="search-alias=popular">CDs &amp; Vinyl</option> . . . </select> 

我不知道HTML,这很容易或不可能做到。 你有什么build议吗?

改变<select>元素的值是一件小事。 是否更新屏幕以反映改变是至关重要的另一个问题。

 'try to get dropdown box/ I think it is working fine, Set element = IE.Document.getElementByID("searchDropdownBox") '.Item(0) <~~ do not need this on the end 'try to change a value of dropdown box/ It does nothing element.selectedIndex = 4 'set to Arts, Crafts and Sewing element.Value = "search-alias=stripbooks" 'set to Books 

如果您使用F8浏览代码,则可以先将searchDropdownBoxselect元素设置为Arts,Crafts和Sewing ,然后通过返回到可见的IE窗口并单击select下拉菜单来确认。 再次点击F8,您可以同样确认select设置为书籍