I'm trying to scraping table from below website. there's a drop down list on the left part of the website and I want to select and click "Professional" in the type section. Below is my VBA code. I have no web developing experience and as far as I can do is only find the table I want and select show 100 items
Sub CopyFromHKEXWebsite()
Dim ie As Object, btnmore As Object, tbl As Object
Dim cnt As Integer
Set ie = CreateObject("internetexplorer.application")
With ie
.Visible = True
.navigate "https://www.hkex.com.hk/Market-Data/Securities-Prices/Debt-Securities?sc_lang=en"
Do
DoEvents
Loop While .ReadyState <> 4 Or .Busy
.Document.getelementsbyclassname("select_items")(0).setAttribute "style", "display: block"
Do
DoEvents
Loop While .Busy 'wait for scriptcode to execute
.Document.getelementsbyclassname("select_items")(0).Children(2).Click
Do
DoEvents
Loop While .ReadyState <> 4 Or .Busy
****'professional
.Document.getelementsbyid("select2")(0).selectedindex = 1
Do
DoEvents
Loop While .Busy 'wait for scriptcode to execute****
Do
Set tbl = .Document.getelementsbyclassname("table_debtsecurities")(0)
Set btnmore = .Document.getelementsbyclassname("loadmore")(0)
cnt = cnt 1
Application.Wait Now TimeValue("00:00:01")
Loop While tbl Is Nothing Or btnmore Is Nothing And cnt < 5 'maximum attempts = 4
Do While btnmore.getattribute("style") = ""
btnmore.Click
Do
DoEvents
Loop While .Busy
Loop
End With
End Sub
CodePudding user response:
First it is not GetElementsById
but GetElementById
(singular Element
without s
) because there cannot be multiple elements with the same ID
(without violating the HTML standard). While for Name
and ClassName
there can exist multiple elements it needs to be plural Elements
with an s
.
Second you find the the combobox by its name="select2"
so we need to use GetElementsByName
<input type="hidden" name="select2" value="ALL">
Dim TypeCombo As Object
Set TypeCombo = .document.GetElementsByName("select2")(0)
TypeCombo.Value = "Professional"
Then the apply button
<div class="etps_apply_btn">APPLY FILTERS</div>
can be found by its so we use
GetElementsByClassName
Dim ApplyButton As Object
Set ApplyButton = .document.GetElementsByClassName("etps_apply_btn")(0)
ApplyButton.Click
and apply a click.