Reputation: 394
I am trying to pull the data from websites. So I want to select 3 drop-down values in below URL but i cant change those values. example i want to select month
<select name="fmonth1" id="fmonth1" class="dropdownboxlang" size="1" style="width:60px;">
<option value="0">MM</option>
<option value="1">Jan</option>
<option value="2">Feb</option>
<option value="3">Mar</option>
<option value="4">Apr</option>
<option value="5">May</option>
<option value="6">Jun</option>
<option value="7">Jul</option>
<option value="8">Aug</option>
<option value="9">Sep</option>
<option value="10">Oct</option>
<option value="11">Nov</option>
<option value="12">Dec</option>
</select>
the error i am getting while changing the value in drop-down. I am trying all the possible ways from two days. Any suggestion would be appreciated.
Public Sub bse()
Dim IE As InternetExplorer
Dim HTML As HTMLDocument
Dim Dropdown As IHTMLElement
Dim dropOption As IHTMLElement
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.Navigate "https://www.bseindia.com/markets/debt/BhavCopyDebt.aspx?expandable=6"
End With
Do
DoEvents
Application.Wait Now() + TimeValue("00:00:01")
Loop Until IE.ReadyState = 4 And Not IE.Busy
Set HTML = IE.Document
HTML.getElementsByName("fmonth1")(0).Value = "1" error line
IE.Quit
End Sub
Upvotes: 1
Views: 345
Reputation: 84465
You can use css id selector in descendant combination with attribute = value selector
#fmonth1 option[value='1']
That is:
ie.document.querySelector("#fmonth1 option[value='1']")
You may need .Click on the end for selected. Can't test that url but also try:
ie.document.querySelector("#fmonth1 option[value='1']").Selected = True
More generally, if you know an element exists, and your syntax is correct, but you are still getting not set then it may be a timing issue where you need a longer wait (e.g. timed loop) before attempting to access, e.g.
Const MAX_WAIT_SEC As Long = 10
Dim t As Date, ele As Object
t = Timer
Do
DoEvents
On Error Resume Next
Set ele = ie.document.querySelector("#fmonth1")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While ele Is Nothing
If Not ele Is Nothing Then
ele.Click 'may be needed to expose options
ie.document.querySelector("#fmonth1 option[value='1']").Selected = True
End If
If inside of a parent iframe/frame you will need to navigate that first e.g.
ie.document.getElementsByTagName("iframe")(0).contentDocument.querySelector("#fmonth1 option[value='1']").Selected = True
I tested the following now I can access the page:
Public Sub MakeSelection()
Dim ie As New InternetExplorer
With ie
.Visible = True
.navigate "https://www.bseindia.com/markets/debt/BhavCopyDebt.aspx?expandable=6"
While .Busy Or .readyState < 4: DoEvents: Wend
.document.getElementsByTagName("iframe")(0).contentDocument.querySelector("#fmonth1 option[value='1']").Selected = True
Stop '<==Delete me later
.Quit
End With
End Sub
Upvotes: 1