通过VBA更改Amazon ComboBox

时间:2015-12-29 13:33:28

标签: html excel vba excel-vba

我尝试编写一个VBA宏,它可以自动化我在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是否有可能,我正在搜索错误的代码部分)

<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,它很容易或不可能。 你有什么提示吗?

1 个答案:

答案 0 :(得分:2)

更改&lt; select&gt; 元素的值是一件小事。是否更新屏幕以反映变化至关重要是另一回事。

'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单步执行代码,则可以先将 searchDropdownBox 选择元素设置为 Arts,Crafts and Sewing 并通过返回可见的IE窗口进行确认单击选择下拉列表。再次点击F8,您可以类似地确认选择设置为图书