I want to select a specific element from the drop-down elements on the amazon.com web page using VBA. I want to perform this where a customer is required to select the quantity of the product they want to add to their shopping cart. How can i choose a different value e.g., 3 from the drop down menu using VBA and display the value into an excel sheet? For now, i am always getting the default value 1 even if i set for a different element to be selected.
Sub Basics_Of_Web_Macro()
Dim myIE As Object
Dim myIEDoc As Object
'Start Internet Explorer
Set myIE = CreateObject("InternetExplorer.Application")
'if you want to see the window set this to True
myIE.Visible = False
'Now we open the page we'd like to use as a source for information
myIE.navigate "https://www.amazon.com/belif-True-Cream-Aqua-Korean/dp/B00H4GOAZO/ref=pd_cart_crc_cko_cp_2_2/139-8277217-3794320?_encoding=UTF8&pd_rd_i=B00H4GOAZO&pd_rd_r=e154e278-8a11-4ab0-8173-5d0dbaff1938&pd_rd_w=Hm8FW&pd_rd_wg=Hpv4X&pf_rd_p=eff166ab-25d2-4f2c-a51d-0a0e86061f9d&pf_rd_r=EVT26E6K7CV8T1QMTY7H&psc=1&refRID=EVT26E6K7CV8T1QMTY7H"
'We wait for the Explorer to actually open the page and finish loading
While myIE.Busy
DoEvents
Wend
'Now lets read the HTML content of the page
Set myIEDoc = myIE.document
'Time to grab the information we want
Set elements = myIEDoc.getElementsByClassName("a-dropdown-prompt")
If elements.Length > 0 Then
Range("A1") = elements(2).innerText
End If
End Sub
A sample of the amazon source code looks like this with about 30 elements in the drop-down menu for the number of quantity one can choose:
...
<div class="a-column a-span12 a-text-left">
<span class="a-dropdown-container"><label for="quantity" class="a-native-dropdown">Qty:</label><select name="quantity" autocomplete="off" id="quantity" tabIndex="-1" class="a-native-dropdown">
<option value="1" selected>1
</option>
<option value="2" selected>2
</option>
<option value="3" selected>3
</option>
...