我尝试编写一个 VBA 宏来自动执行 Internet Explorer 中的工作。我的目标是捕获已经打开的亚马逊网页,并为“Books”设置下拉菜单的值。 目前我能够捕获正在运行的亚马逊网站,但我不知道如何更改下拉菜单的值。
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
在这里您可以看到 Amazon 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 & Games</option>
<option value="search-alias=arts-crafts">Arts, Crafts & 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 & Vinyl</option>
.
.
.
</select>
我不懂 HTML,这可能很容易,也可能不可能。 你有什么建议吗?
最佳答案
更改 元素的值是一件小事。更新屏幕以反射(reflect)变化是否也至关重要则是另一回事。
'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 选择元素设置为艺术、工艺品和缝纫,然后返回到可见的 IE 窗口并确认这一点单击选择下拉菜单。再次点击 F8,您可以类似地确认选择已设置为图书。
关于html - 通过 VBA 更改 Amazon ComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34512169/