VBA Select from dropdown list while filling webform - html

I'm quite new to VBA and HTML. I try to write a VBA macro which fill in a web form.
I am struck at a point where I have to select value from the dropdown list.
Sub test()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate ("https://www.fakturowo.pl/wystaw")
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
Set doc = IE.Document
doc.getElementById("rodzaj").Value = 26
doc.getElementById("miasto").Value = "XYZ"
doc.getElementById("nazwa_sprzedawca").Value = "XYZ"
doc.getElementById("ulica_sprzedawca").Value = "XYZ"
Here is the HTML:
<select name='rodzaj' id="rodzaj" onchange="javascript:submit();">
<option value='47'>Dobowy raport fiskalny</option>
<option value='24'>Dowód dostawy</option>
<option value='27'>Dowód dostawy (tylko ilość)</option>
<option value='7'>Dowód wewnętrzny</option>
<option value='42'>Eksport towarów</option>
<option value='48'>Eksport usług</option>
<option value='49'>Eksport usług (VAT-EU 28b)</option>
<option value='62'>Faktura</option>
<option value='21'>Faktura (bez vat, dawny rachunek)</option>
<option value='45'>Faktura (odwrotne obciążenie bez vat)</option>
<option value='35'>Faktura (odwrotne obciążenie z vat)</option>
<option value='69'>Faktura MPP (mechanizm podzielonej płatności)</option>
<option value='0' selected>Faktura VAT</option>
<option value='44'>Faktura VAT MP</option>
<option value='26'>Faktura VAT RR</option>
<option value='5'>Faktura VAT marża</option>
Many thanks!
Gosia

You must trigger the html change event of the dropdown. To do that you must save the html dropdown element do an object variable. So you can give it to the needed function to trigger html events.
I have commented the relevant code parts:
Sub test()
Dim IE As Object
Dim nodeDropDown As Object 'Needed to set the dropdown as DOM Object
Dim doc As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate ("https://www.fakturowo.pl/wystaw")
Do While IE.ReadyState <> 4: DoEvents: Loop
Set doc = IE.Document
'set the dropdown as DOM object
Set nodeDropDown = doc.getElementById("rodzaj")
'Change the value of the dropdown
nodeDropDown.Value = 26
'Trigger the html change event of the dropdown
Call TriggerEvent(doc, nodeDropDown, "change")
'Wait to load the page new after changing the dropdown value
Application.Wait (Now + TimeSerial(0, 0, 5))
doc.getElementById("miasto").Value = "XYZ"
doc.getElementById("nazwa_sprzedawca").Value = "XYZ"
doc.getElementById("ulica_sprzedawca").Value = "XYZ"
End Sub
This is the procedure to trigger a html event
Private Sub TriggerEvent(htmlDocument As Object, htmlElementWithEvent As Object, eventType As String)
Dim theEvent As Object
htmlElementWithEvent.Focus
Set theEvent = htmlDocument.createEvent("HTMLEvents")
theEvent.initEvent eventType, True, False
htmlElementWithEvent.dispatchEvent theEvent
End Sub

Related

how to select an option from a dropdown list in a webpage using the inner text

How to select an option from a dropdown list in a webpage using the inner text
below is vba code
Sub navigation_site_with_part_number()
Dim eRow As Long
Dim ele As Object
Set sht = Sheets("Sheet1")
RowCount = 1`enter code here
eRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Set objIE = CreateObject("InternetExplorer.Application")
requestnumber = InputBox("Enter the request number.")
With objIE
.Visible = True
.navigate "*http://www.auto.com*"
Do While .Busy Or _
.readyState <> 4
DoEvents
Loop
.document.getElementsByid("inspNumber").selectedinnertext = 2
.document.getElementById("InspQuery_Search").Click
Do While .Busy Or _
.readyState <> 4
DoEvents
Loop
End With
Below is Html code:
div id="RequestNumberGroup" class="form-group">
<label for="TextRequestNumber">Request Number:</label>
<select class="form-control" id="inspNumber" name="inspNumber">
<option value=""></option>
<option value="813">20190205</option>
<option value="1034">20190426</option>
<option value="1059">20190451</option>
<option value="1061">20190453</option>
<option value="1064">20190456</option>
<option value="1065">20190457</option>
I expected to select the option in dropdownlist with requestnumber not option value
There isn't a select by text option in IE implementation.
1) Either gather collection/nodelist of options and loop comparing innerText against desired text string then select if found and Exit For
Dim options As Object, i As Long, found As Boolean
Set options = ie.document.querySelectorAll("#inspNumber options")
For i = 0 To options.length - 1
If options.item(i).innerText = "target string" Then
options.item(i).Selected = True
found = True
Exit For
End If
Next
If Not found Then
'do something if not found
End If
2) Use selenium vba wrapper which does implement this functionality
Dim ele As Select
Set ele = driver.FindElementById("inspNumber").AsSelect
ele.SelectByText "Option Text"
N.B. Of course there are using attributes values and selectedIndex methods but you didn't want those.

I can't select multiple items in dropdown list web VBA

I am trying to fill in a form from an intranet page.
I have managed to highlight the element from a dropdown list I want to select but not at the same time. I want to select three in list
Here's my code:
Sub GetLTRTable()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.application")
ie.Visible = True
ie.navigate "myurl"
Do Until ie.ReadyState = READYSTATE_COMPLETE
DoEvents
Loop
Dim Fields As Object
Set Fields = ie.Document.all.Item("ADFields")
Fields.Checked = True
Dim organization As Object
Set organization = ie.Document.all.Item("Org")
organization.selectedindex = 0
Dim Modality As Object
Set Modality = ie.Document.getElementsByName("Modality")(0)
With Modality.Value = "'GDXE'"
Modality.Value = "'ABUS'"
Modality.Value = "'A&S'"
End With
ie.Document.getElementsByName("Action").Item(1).Click
Here's the source code (note that I have just put an extract since there are a lot of modalities):
<select name="Modality" multiple="" size="4"><option value="'ALL'" selected="">ALL</option>
<option value="'A&S'">A&S</option>
<option value="'ABUS'">ABUS</option>
<option value="'ACS'">ACS</option>
<option value="'ANES'">ANES</option>
</select>
Thanks for the help
Corentin
If you want to use by value then use attribute = value css selectors
With ie.document
.querySelector("[value='ABUS']").Selected = True
.querySelector("[value='GDXE']").Selected = True
End With
This should prove a fast selector method.
Also, use a proper page wait:
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
In this case, you don't need to change the value of Modality, but set the options to "selected":
Modality.getElementsByTagname("option").item(0).selected = "selected"

Object Variable or With Block Variable not set getelementsbyname vba

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
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

Excel VBA Select Option from Website Dropdown List while filling webform

I am trying to automate this website using VBA excel. I am struck at a point where I have to select value from the dropdown box. I am very much new to VBA and tis is my 1st such project. this is what I have coded to select the value.
Sub automaticformfilling_ASDA()
Dim ie As Object
Set ie = CreateObject("internetexplorer.application")
'to make sure that the website is properly loaded
With ie
.Visible = True
.navigate "https://www.argos-pet-insurance.com/quoteAndBuy.do?e=e1s1&curPage=captureDetails&rakT=1510852044896.1391473424.994101.1731.881880349.846|tsid:9904"
Do While .Busy
DoEvents
Loop
Do While .readyState <> 4
DoEvents
Loop
End With
Set Title = i.e.document.getElementById("yourDetailsPolicyHolderTitle")
For i = 1 To Title.Options.Length
If Title.Options(i).Text = "Mrs" Then
Exit For
End If
Next i
End Sub
Here is the HTML of that section:
<select name="policyHolder.title" class="select-large" id="yourDetailsPolicyHolderTitle" data-di-field-id="policyHolderTitle">
<option selected="selected" value="">Please select</option>
<option value="NWA_PET_T5">Dr</option>
<option value="NWA_PET_T3">Miss</option>
<option value="NWA_PET_T1">Mr</option>
<option value="NWA_PET_T2">Mrs</option>
<option value="NWA_PET_T4">Ms</option>
</select>
As per the comment from SJR and also a typo I found in your code, if you replace your code with this then it should work:
Sub automaticformfilling_ASDA()
Dim ie As Object
Set ie = CreateObject("internetexplorer.application")
'to make sure that the website is properly loaded
With ie
.Visible = True
.navigate "https://www.argos-pet-insurance.com/quoteAndBuy.do?e=e1s1&curPage=captureDetails&rakT=1510852044896.1391473424.994101.1731.881880349.846|tsid:9904"
Do While .Busy
DoEvents
Loop
Do While .readyState <> 4
DoEvents
Loop
End With
Set Title = ie.document.getElementById("yourDetailsPolicyHolderTitle")
Title.selectedIndex = 4
End Sub
You entered i.e.document.getElementByID, where you should have ie.document.

Select all options in HTML using VBA

So I am trying to select all the options on a website using VBA.
the options sometimes changes but i want to select all of them.
the below is the html code for the selection menu.
<select class="locationSelect" id="ReceiptLocations" multiple="multiple" name="ReceiptLocations" size="10"><option value="7">Bayhurst 1</option>
<option value="157">Union Parkway Belt</option>
<option value="260">Union SSMDA</option>
<option value="261">Union WDA</option>
<option value="244">Welwyn</option>
<option value="83343">Whitewood Receipt</option>
</select>
My current code in excel is as follow:
Sub extract()
Dim oIE As Object
Dim ohtml As HTMLDocument
Dim objOption As Object
Set oIE = CreateObject("InternetExplorer.Application")
oIE.Navigate ("http://itbidfloors.transcanada.com/")
oIE.Visible = True
' Wait while IE loading
Do
DoEvents
Loop Until oIE.ReadyState = 4
For Each objOption In oIE.Document.getElementsByClassName("locationSelect").getElementsByID("ReceiptLocations").Options
objOption.Selected = True
Next
End Sub
The error i run into is the object doesnt support this property or method.
i am using a for loop in this case because i want to select all the options that are available.
What am i doing wrong here?
Thanks in advance
Sub extract()
Dim oIE As Object
Dim ohtml As HTMLDocument
Dim objOption As Object
Set oIE = CreateObject("InternetExplorer.Application")
oIE.Navigate ("http://itbidfloors.transcanada.com/")
oIE.Visible = True
' Wait while IE loading
Do
DoEvents
Loop Until oIE.ReadyState = 4
For Each objOption In oIE.Document.getElementsByClassName("locationSelect").getElementsByID("ReceiptLocations").Options
objOption.Selected = True
Next
End Sub