2015-12-29 90 views
2

我嘗試編寫一個VBA宏,該宏在Internet Explorer中自動完成我的工作。我的目標是趕上已經打開的亞馬遜網頁,併爲「書籍」創建下拉菜單的值。 目前我能夠捕捉正在運行的亞馬遜網站,但我不知道如何更改下拉菜單的值。通過VBA更改Amazon ComboBox

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 ,這很容易或不可能做到。 你有任何提示嗎?

+0

也許'element.Value =「search-alias = stripbooks」' – Jeeped

+0

嗨, 不幸的是,它也沒有做什麼。 (也沒有錯誤返回) –

+0

好吧,現在看到 - IT正在工作,它只是沒有刷新在屏幕上。對不起,謝謝很多Jeeped! –

回答

2

更改的值<選擇>元素是一個小事。是否更新屏幕以反映更改也是另一回事。

'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你的代碼步驟可以先通過返回可見的IE窗口,然後點擊選擇下拉設置searchDropdownBox選擇元素藝術,工藝和縫紉和確認。再次點擊F8,您可以同樣確認選擇設置爲書籍