Loop through dropdown browsermenu with VBA - html

I need to loop through a dropdown list of a specific website and download the latest file for each entry. I managed to open the website and click the "submit" button, but I can´t find a solution to loop through all the dropdown entries.
Dim reportnr As String
Dim internetadress As String
Dim btn As Object
Dim IE As SHDocVw.InternetExplorer
reportnr = 10
internetadress = adress & reportnr
Set IE = CreateObject('InternetExplorer.Application')
IE.Visible = True
IE.navigate internetadress
Do While IE.Busy
Application.Wait DateAdd("s", 2, Now)
Loop
While IE.Busy
DoEvents
Wend
For Each btn In IE.document.getElementsByClassName("btn btn-primary")
If btn.name = "submit" Then
btn.Click
End If
Next btn
The code on the website regarding the dropdown below.
I tried several approchoaches, but I´ll get error or just nothing happens. Last thing I tried is this :
Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub SelectQuantity()
Dim ie As New InternetExplorer, numberOfOptions As Long, i As Long
With ie
.Visible = True
.Navigate2 "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"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
numberOfOptions = .querySelectorAll("#quantity option").Length 'gather option tag element children of parent select element with id quantity
For i = 1 To numberOfOptions
.querySelector("#quantity [value='" & i & "']").Selected = True
ActiveSheet.Cells(i, 1) = i
Next
Stop
End With
End With
End Sub
Source: VBA loop through dropdown elements from web page and download to excel sheet
Thanks in advance for help.

From the website you provide, you can try the following code to loop through the dropdown list to choose the option you want:
Set lists = IE.document.getElementsByClassName("active-result") 'get all the options li elements
For Each liElement In lists
If liElement.innerText = "something" Then
liElement.setAttribute "class", "active-result result-selected"
End If
Next liElement
I find that the chosen option has different class which is active-result result-selected, so I think maybe you can change the class to select the option.

Related

Select HTML item using VBA Excel

I tried to extract somes information from ALibaba.com but i didn't success to select Supplier in the list
Here is the html code
Set Post = IEdoc_ALi.getElementsByClassName("ui-searchbar-type-option").Item
Post.all(2).innerText = "Suppliers"
Thanks in advance
It's a link that needs clicking. You can use a css attribute = value selector to target the relevant node and then apply the click method.
Option Explicit
Public Sub SelectSuppliers()
Dim ie As SHDocVw.InternetExplorer
Set ie = New SHDocVw.InternetExplorer
With ie
.Visible = True
.Navigate2 "https://www.alibaba.com/"
While .Busy Or .readyState <> 4: DoEvents: Wend
.document.querySelector("[data-value=suppliers]").Click
Stop '<Delete me later
End With
End Sub

VBA loop through dropdown elements from web page and download to excel sheet

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>
...
You can usually use an attribute = value selector to target the option of choice by its value attribute value
myIEDoc.querySelector("#quantity [value='3']").selected = True
You can concatenate that value in
Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub SelectQuantity()
Dim ie As New InternetExplorer
Const QTY As Long = 3
With ie
.Visible = True
.Navigate2 "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"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
.querySelector("#quantity [value='" & QTY & "']").Selected = True
ActiveSheet.Range("A1") = QTY
Stop
End With
End With
End Sub
Loop over all
Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub SelectQuantity()
Dim ie As New InternetExplorer, numberOfOptions As Long, i As Long
With ie
.Visible = True
.Navigate2 "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"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
numberOfOptions = .querySelectorAll("#quantity option").Length -1 'gather option tag element children of parent select element with id quantity
For i = 0 To numberOfOptions
.querySelector("#quantity [value='" & i & "']").Selected = True
ActiveSheet.Cells(i +1, 1) = i '
Next
Stop
End With
End With
End Sub
Matching closer to yours
Public Sub Basics_Of_Web_Macro()
Dim myIE As Object
Const QTY As Long = 3
'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.navigate2 "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 Or myIE.readyState < 4: DoEvents: Wend
With myIE.document
.querySelector("#quantity [value='" & QTY & "']").Selected = True
ActiveSheet.Range("A1") = QTY
Stop
End With
End Sub

I want to click the button in VBA

I am a beginner and non-technical person, I am completely stuck here
I want to click the New Project button using VBA
Here is the inspect Element for New project button
<button tabindex="0" class="xui-button xui-button-create xui-button-small" type="button" data-automationid="project-modal-new-button">New project</button>
Below is the code, I have created so far, it's running without any issues and it will take me to the project tab from the Project Tab I need to click the new Project Button using VBA
Sub Xero()
Dim i As SHDocVw.InternetExplorer
Set i = New InternetExplorer
i.Visible = True
i.navigate ("https://login.xero.com/")
Do While i.readyState <> READYSTATE_COMPLETE
Loop
Dim idoc As MSHTML.HTMLDocument
Set idoc = i.document
idoc.all.UserName.Value = "XXXXX"
idoc.all.Password.Value = "123"
Dim ele As MSHTML.IHTMLElement
Dim eles As MSHTML.IHTMLElementCollection
Set eles = idoc.getElementsByTagName("button")
For Each ele In eles
If ele.ID = "submitButton" Then
ele.Click
Application.Wait (Now + TimeValue("0:00:10"))
Else
End If
Next ele
i.navigate ("https://projects.xero.com/?CID=!QhJgj")
Do While i.readyState <> READYSTATE_COMPLETE
Loop
End Sub
I want to click the New project button to move further
Try using a css class selector (same idea as getElementsByClassName but faster)
While i.Busy Or i.readyState < 4: DoEvents: Wend
Application.Wait Now + Timeserial(0,0,2)
i.document.querySelector(".xui-button-create").click
Also, use proper page waits after .click and .navigate i.e.
While i.Busy Or i.readyState < 4: DoEvents: Wend
So, have that proper wait between .navigate and the attempt to click the button and again after clicking the button.

Can't populate results from a website using vba

I've created a script in vba using IE to get the content poputaled upon selecting an option from dropdown. In the site's landing page there is a dropdown named Type. When I click on Type, I can see few options there and I would like to click on corporate bond among them. When the result are generated I would like to parse the WKN.
I've tried to populate results using my current script by clicking on the dropdown and selecting an option from there. However, it clicks on the dropdown and select the desired option but can't produce any result in InternetExplorer.
How can I populate and parse the result selecting an option from dropdown?
My attempt so far:
Sub SelectItem()
Const link = "https://www.boerse-stuttgart.de/en/tools/product-search/bonds/"
Dim IE As New InternetExplorer, Html As HTMLDocument, post As Object, T As Date
With IE
.Visible = True
.navigate link
While .Busy Or .readyState < 4: DoEvents: Wend
Set Html = .document
End With
Application.Wait Now + TimeValue("00:00:15")
Html.querySelector("#bsg-filters-btn-bgs-filter-3 .bsg-btn__icon").Click
Application.Wait Now + TimeValue("00:00:05")
Html.querySelector("#bsg-filters-menu-bgs-filter-3 .bsg-scrollbox label[for='bsg-checkbox-3053']").Click
Application.Wait Now + TimeValue("00:00:05")
Html.querySelector(".bsg-dropdown__footer button[type='button']").Click
End Sub
You are familiar with timed loops so you can add timeouts to the following. I use css selectors through out to target elements. I monitor for the disappearance of the whirring circles as measures of page load at various points. At the end I monitor for presence of results items nodeList nodes.
Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub GetCodes()
Dim ie As New InternetExplorer, btn As Object, items As Object, i As Long
With ie
.Visible = True
.Navigate2 "https://www.boerse-stuttgart.de/en/tools/product-search/bonds"
While .Busy Or .readyState < 4: DoEvents: Wend
Do
Loop Until .document.querySelectorAll(".bsg-loader-ring__item").Length = 0
.document.querySelector("#bsg-filters-btn-bgs-filter-3").Click
.document.querySelector("#bsg-checkbox-3053").Click
Set btn = .document.querySelector("#bsg-filters-menu-bgs-filter-3 .bsg-btn__label")
Do
Loop While btn.innerText = "Close"
btn.Click
Do
Loop Until .document.querySelectorAll(".bsg-loader-ring__item").Length = 0
Dim count As Long
Do
On Error Resume Next
Set items = .document.querySelectorAll(".bsg-table__tr td:first-child")
count = items.Length
On Error GoTo 0
Loop While count = 0
For i = 0 To items.Length - 1
Debug.Print items.item(i).innerText
Next
.Quit
End With
End Sub

Why my google search macro is returning error 424 - Object required?

I have this simple macro that opens up IE and type a word in search box. It was working before, but now is returning
Error 424 - Object required.
Any ideas on what is happening? Is there any lib reference that I'm missing?
Sub AcessaPaginaDados()
Dim xobj As HTMLDivElement
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate ("https://www.google.com/")
Do While ie.Busy Or ie.readyState <> 4
DoEvents
Loop
Set xobj = ie.document.getElementById("lst-ib")
Call xobj.setAttribute("value", "teste")
End Sub
That id doesn't appear against the input box when I open google. You can use an attribute = value selector to target the title attribute of the search box. Right-click inspect the search box and see what your local language attribute value is for title=. In U.K. the value is Search. Use whatever your local value is.
Element html:
Other:
Also, .navigate2 is the preferred method.
If the page is slow loading you may need a loop until element present.
Option Explicit
Public Sub AcessaPaginaDados()
Dim xobj As HTMLDivElement
With CreateObject("InternetExplorer.Application")
.Visible = True
.Navigate2 "https://www.google.com/"
While .Busy Or .readyState < 4: DoEvents: Wend
Set xobj = .document.querySelector("[title=Search]")
xobj.Value = "teste"
Stop
.Quit
End With
End Sub