I am trying to use VBA to select from a dropdown list. The HTML code is below
<select name="template" class="chzn-select stdText allow_single_deselect"
id="template" style="width: 315px; display: none; visibility: visible;"
onchange="newDoc.doAfterTemplateNew('templateRow',this);" size="3"
data-automation-id="authorTemplateDropdown" data-placeholder="Choose template...">
<option></option>
<option value="1">1. Company_Comment (CC)</option>
<option value="3">1. Company_Flash (CF)</option>
<option value="79">1. Company_Report (CR)</option>
<option value="21">2. Sector_Comment (SC)</option>
Right now I am trying to use:
.document.querySelector("Select[name=template] option[value=1]").Selected = True
However, I am getting error 8070000c. Thank you for the help!
This worked for me. Note quotes around option value attribute
Sub tester()
'Added reference to Microsoft HTML Object Model
Dim doc As New HTMLDocument, opt As Object, slct As Object
doc.body.innerHTML = Range("A1").Value 'load HTML from cell for testing
Set slct = doc.querySelector("select[name=template]") 'select object
Set opt = doc.querySelector("select[name=template] option[value='1']") 'option
Debug.Print "before", slct.selectedIndex ' >> -1
opt.Selected = True
Debug.Print "after", slct.selectedIndex ' >> 1
End Sub
HTML in A1:
<select name="template" id="template" size="3" data-placeholder="Choose template...">
<option></option>
<option value="1">1. Company_Comment (CC)</option>
<option value="3">1. Company_Flash (CF)</option>
<option value="79">1. Company_Report (CR)</option>
<option value="21">2. Sector_Comment (SC)</option>
</select>
Related
Hi Again The above code is working for week selection but not to select time
<td style="white-space:nowrap;">Master Timings</td>
<td>
<select name="ddlSTSun" id="ddlSTSun" onChange="return ChangeAllSTET(frmRosterInfoTLEmp,'ddlSTSun','ddlST1Sun02');" style="font-family:Arial;font-size:9pt;">
<option value="01:00">01:00</option>
<option value="02:00">02:00</option>
<option value="03:00">03:00</option>
<option value="04:00">04:00</option>
<option value="05:00">05:00</option>
<option value="06:00">06:00</option>
<option value="07:00">07:00</option>
My VBA code is
ie.Document.getElementsByTagName("iframe")(0).contentDocument.querySelector("[name=cmbWeeks]").selectedIndex = "Week:-2 19-May-2019"
ie.Document.getElementsByTagName("iframe")(0).contentDocument.querySelector("[name=cmbWeeks]").FireEvent ("onchange")
Sleep 5000
ie.Document.getElementsByTagName("iframe")(0).contentDocument.querySelector("[name=ddlSTSun]").selectedIndex = "12:00"
ie.Document.getElementsByTagName("iframe")(0).contentDocument.querySelector("[name=ddlSTSun]").FireEvent ("onchange")
There is an id so use that first-off. Then selectedIndex takes the index position e.g. 1,2 etc not the time value.
ie.Document.getElementsByTagName("iframe")(0).contentDocument.querySelector("#ddlSTSun").selectedIndex = 1
You can use an attribute=value selector to specify the time:
ie.Document.getElementsByTagName("iframe")(0).contentDocument.querySelector("#ddlSTSun [value='12:00']").selected = True
Event (if required):
ie.Document.getElementsByTagName("iframe")(0).contentDocument.querySelector("#ddlSTSun").FireEvent "onchange"
I am trying to fetch the names and values of locations from a website page.
For example: I want to take the value 10 and label " Johannesburg OR Tambo International Airport" and insert it into cell B3 and B4 respectively and then loop it for all optgroups. I get an error "Object doesn't support this property or method." Im sure my code has multiple issues. any assistance will be greatly appreciated.
My code is as follows:
Sub test1()
''''''''''''''''''''''''''''This part states the variables and their dimenstions.
Dim appIE As Object
Dim ws As Worksheet
Dim wb As Workbook
Dim o
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
i = 2
Set wb = Application.Workbooks("Test2")
Set ws = wb.Worksheets("Europcar Branches")
Set appIE = CreateObject("internetexplorer.application")
'Navigate to Europcar
'Open internet explorer
With appIE
.Navigate "https://www.europcar.co.za"
.Visible = True
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Application.Wait (Now + TimeValue("0:00:03"))
Do While appIE.busy
DoEvents
Application.Wait (Now + TimeValue("0:00:05"))
Loop
Application.Wait (Now + TimeValue("0:00:02"))
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Set entry = appIE.document.getElementById("PickupBranch_BranchID_id")
For Each o In entry.getElementsByName("optgroup")
Cells(i, 3).Value = o.Value
For Each p In entry.getElementsByName("optgroup").Options
Cells(i, 4).Value = p.innerText
i = i + 1
Exit For
Next
Exit For
Next
'
'.Navigate "https://www.europcar.co.za"
'.Visible = True
Application.Wait (Now + TimeValue("0:00:01"))
Do While appIE.busy
DoEvents
Application.Wait (Now + TimeValue("0:00:03"))
Loop
End With
appIE.quit
Set appIE = Nothing
End Sub
A section of Html is as follows:
<select name="PickupBranch_BranchID" class="pick-up-select responsive-select" id="PickupBranch_BranchID_id" style="display: none;" data-placeholder="Pickup Location">
<option value=""></option>
<optgroup value="0" label="Airports">
<option value="10">Johannesburg OR Tambo International Airport</option>
<option value="20">Cape Town International Airport</option>
<option value="76">King Shaka International Airport</option>
<option value="48">Lanseria Airport</option>
<option value="89">Bloemfontein Airport</option>
<option value="70">East London Airport</option>
<option value="61">George Airport</option>
<option value="91">Kimberley Airport </option>
<option value="14">Polokwane Airport</option>
<option value="95">Kruger Mpumalanga Int Airport</option>
<option value="138">Malelane Airport</option>
<option value="79">Margate Airport</option>
<option value="44">CSIR Pretoria</option>
<option value="13">Pietermaritzburg Airport</option>
<option value="7">Port Elizabeth Airport</option>
<option value="84">Richards Bay Airport</option>
<option value="75">Umtata Airport</option>
<option value="103">Upington Airport</option>
<option value="52">Wonderboom Airport</option>
<option value="46">Germiston Rand Airport</option>
</optgroup>
<optgroup value="3" label="Gauteng">
<option value="133">Boksburg Easyway</option>
<option value="42">Braamfontein</option>
<option value="134">Bryanston Easyway </option>
<option value="43">Centurion</option>
<option value="135">Constantia Kloof Easyway</option>
<option value="45">Fourways</option>
<option value="154">Johannesburg Parkstation</option>
<option value="125">Kramerville</option>
<option value="121">Meadowdale</option>
<option value="50">Megawatt Park</option>
<option value="155">Menlyn Easyway</option>
<option value="47">Mogale City (Krugersdorp Agency)</option>
<option value="11">Pretoria Hatfield</option>
<option value="53">Randburg</option>
<option value="161">Rosebank Gautrain Station</option>
<option value="158">Sandton Gautrain Station</option>
<option value="55">Sandton Town</option>
<option value="59">Vanderbijlpark</option>
</optgroup>
</select>
The following shows you how to do for one drop down (it gathers all the optgroups within). It avoids using a browser and goes with the faster xmlhttp request. I use getElementById, to get the parent select element, and then getElementsByClassName to retrieve the child option tag elements. I loop from 1 to avoid the empty first element.
References (VBE > Tools > References):
Microsoft HTML Object Library
VBA:
Option Explicit
Public Sub GetOptions()
Dim html As Object, ws As Worksheet, headers()
Dim i As Long, r As Long, c As Long, numRows As Long
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set html = New HTMLDocument
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://www.europcar.co.za/", False
.send
html.body.innerHTML = .responseText
Dim pickupBranches As Object, pickupBranchResults()
Set pickupBranches = html.getElementById("PickupBranch_BranchID_id").getElementsByTagName("option")
headers = Array("Pickup Location", "option value")
numRows = pickupBranches.Length - 1
ReDim pickupBranchResults(1 To numRows, 1 To 2)
For i = 1 To numRows
pickupBranchResults(i, 1) = pickupBranches.item(i).innerText
pickupBranchResults(i, 2) = pickupBranches.item(i).Value
Next
With ws
.Cells(1, 1).Resize(1, UBound(headers) + 1) = headers
.Cells(2, 1).Resize(UBound(pickupBranchResults, 1), UBound(pickupBranchResults, 2)) = pickupBranchResults
End With
End With
End Sub
I'm looking for a way to extract data from the available options from a website dropdown box, specifically the second optgroup "All fund companies".
extract of HTML code I'm scraping
</div><div class="large-5 medium-5 columns spacer-bottom padding-left-none"><div class="select_wrap"><select id="search-company" name="companyid" class="default">
<option value="">Search by company</option>
<optgroup label="Popular companies">
<option value="4">Hargreaves Lansdown</option>
<option value="1908">Lindsell Train</option>
<option value="55">Jupiter</option>
<option value="191">Legal & General</option>
</optgroup>
<optgroup label="All fund companies">
<option value="218">Aberdeen</option>
<option value="1080">Aberforth Unit Trust Managers</option>
<option value="141">Allianz Global Investors</option>
<option value="3472">Alquity Investment Management Limited</option>
<option value="1324">Amati Global Investors Ltd</option>
VBA:
Set htmlObj = html.getElementById("search-company")
For Each Child In htmlObj.getElementByClassName("optgroup")(1).Children
sqlId = Child.Value
sqlCompany = Child.innerText
Debug.Print (sqlId & " - " & sqlCompany)
Next
Thanks Jeeped...
This is the code that managed to solve the problem... not sure if it's the most efficient way of doing it, but it works :)
Set htmlObj = html.getElementById("search-company")
For Each Child In htmlObj.Children
If Child.Label = "All fund companies" Then Set htmlObj2 = Child
Next
For Each Child In htmlObj2.Children
sqlId = Child.Value
sqlCompany = Child.innerText
Debug.Print (sqlId & " - " & sqlCompany)
Next
I'm trying to populate a <select> with <option>'s on a site with Classic ASP/VBScript. The values are read and taken from an SQL Server Database and the code for that looks similar to:
SET rows = dbc.execute(SQL)
IF NOT rows.EOF THEN
DO WHILE NOT rows.EOF
%>
<option value="<%=rows("specialty")%>"><%=rows("specialty")%></option>
<%
rows.moveNext
LOOP
ELSE
END IF
rows.close
SET rows = NOTHING
The problem I have is that only one side of the <%=rows("specialty")%> seems to evaluate.
With <option value="<%=rows("specialty")%>"><%=rows("specialty")%></option> I get:
<option value="fromRow1"></option>
<option value="fromRow2"></option>
<option value="fromRow3"></option>
With <option value="test"><%=rows("specialty")%></option> I get:
<option value="test">fromRow1</option>
<option value="test">fromRow2</option>
<option value="test">fromRow3</option>
What should one do to mitigate this issue?
Try this:
Dim a
Do while not rows.Eof
a = rows.Collect("specialty")
Response.Write("<option value=""" & Replace(a, """", """) & """>" & a & "</option>")
rows.MoveNext
Loop
What is the datatype of specialty ?
try this:
DO WHILE NOT rows.EOF
specialty = rows("specialty")
%>
<option value="<%=specialty%>"><%=specialty%></option>
<%
rows.moveNext
LOOP
Try this:
<option value='<%=rows("specialty")%>'><%=rows("specialty")%></option>
it might be getting confused with the doubles quotes. But it has been a while since I've worked in Classic ASP
I am using select tag of html and having multiple attribute. and now I am fetching of selected option from the select tag using asp classic. but getting 500 internal server error.
<%
response.write(request("evtCategory").Count)
response.write(request("evtCategory"))
%>
<form id="frmSearch" method="post" >
<select name="evtCategory" multiple>
<option value="0">--Select Category--</option>
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
<option value="4">Fourth</option>
<option value="5">Fifth</option>
</select>
<input type="submit" value="Search" />
</form>
I don't think Count exists in Classic ASP, that looks like ASP.NET code.
Multiple selected values will be returned as a comma separated string.
You can use Split to get the selected values as an array.
Dim values, i
values = Split(Request.Form("evtCategory"), ",")
For i = 0 To UBound(values)
Response.Write("Value " & i & " = " & values(i) & "<br />")
Next