Pull data from HTML dropdown - html

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

Related

Html select multiple returns last value

I have gone through this and several other examples the code simply doesn't work when I pprint on the backend of django.
<select name="multiple-select[]" id="multiple-select" multiple>
<option value="1">Books</option>
<option value="2">Movies, Music & Games</option>
<option value="3">Electronics & Computers</option>
<option value="4">Home, Garden & Tools</option>
<option value="5">Health & Beauty</option>
<option value="6">Toys, Kids & Baby</option>
<option value="7">Clothing & Jewelry</option>
<option value="8">Sports & Outdoors</option>
</select>
When I select multiple items, the last item is what will be printed out not an array of items as expected.

Firefox not handling Applescript to select from drop down list same as Google Chrome

I am using an Applescript to tab to and select an item from a dropdown box on a form, and return to the starting field. Using Firefox 57.0, both "Cakes, large" (the first item with the first letter "C") and "Cocktails" get selected. Using Google Chrome 63.0 only "Cocktails" gets selected, which is what I want. Any ideas how to get the desired result in Firefox?
Applescript:
tell application "System Events"
tell application "System Events" to key code 48
tell application "System Events" to key code 49
keystroke "Cocktails"
tell application "System Events" to key code 48 using shift down
tell application "System Events" to key code 124 using command down
end tell
HTML code:
<div class="editor-field">
<select class="category-multi" id="RecipeTypes" multiple="multiple" name="RecipeTypes" style="display: none"><option selected="selected" value="85">Fried doughs</option>
<option selected="selected" value="64">Mousses, trifles, custards & creams</option>
</select>
<select class="form-control category-ddl" id="RecipeTypesDDL" name="RecipeTypesDDL"><option value=""></option>
<option value="41">Baked & steamed desserts</option>
<option value="42">Beverages / drinks (no-alcohol)</option>
<option value="82">Bread & buns, sweet</option>
<option value="43">Bread & rolls, savory</option>
<option value="44">Brownies, slices & bars</option>
<option value="45">Cakes, large</option>
<option value="46">Cakes, small</option>
<option value="47">Candy / sweets</option>
<option value="49">Cheesecakes</option>
<option value="50">Children & baby food</option>
<option value="51">Chili</option>
<option value="52">Chutneys, pickles & relishes</option>
<option value="53">Cocktails / drinks (with alcohol)</option>
<option value="54">Cookies, biscuits & crackers</option>
<option value="55">Crumbles, cobblers, crisps & bettys</option>
<option value="56">Curry</option>

Viewing short values of chosen options in multiselect

below I attached some piece of code from https://semantic-ui.com/modules/dropdown.html: (Multiple Selection)
<select name="skills" multiple="" class="ui fluid dropdown">
<option value="">Skills</option>
<option value="angular">Angular</option>
<option value="css">CSS</option>
<option value="design">Graphic Design</option>
<option value="ember">Ember</option>
<option value="html">HTML</option>
<option value="ia">Information Architecture</option>
<option value="javascript">Javascript</option>
<option value="mech">Mechanical Engineering</option>
<option value="meteor">Meteor</option>
<option value="node">NodeJS</option>
<option value="plumbing">Plumbing</option>
<option value="python">Python</option>
<option value="rails">Rails</option>
<option value="react">React</option>
<option value="repair">Kitchen Repair</option>
<option value="ruby">Ruby</option>
<option value="ui">UI Design</option>
<option value="ux">User Experience</option>
</select>
The question is:
Is it possible to show short names of chosen options ? I mean something like:
Options are: keyboard, mouse, monitor, laptop
After chosing options keyboard, mouse shown are: K, M
Is it possible to do it?
I am not familiar with angular or semantic, but it is fairly easy using vanilaJS. You are welcome to test this code on semantic-ui website. Choose to items in the multi-select section then run this in the console:
var items = document.querySelectorAll(".ui.label.transition.visible");
items.forEach(function(item){
var icon = document.createElement("i");
icon.className = "delete icon";
item.innerText = item.innerText[0];
item.appendChild(icon);
});
For sure to use it with angular you would like to modify my code but that is one possible approach.

VBA code to select from an HTML drop down, using value rather than index

I'm fairly new to VBA and HTML. I am writing a VBA macro which navigates through a website, before filling in a web form.
During navigation, the script needs to pick an option from a drop-down menu. I am currently using the following (no doubt awful, but functional) code, which finds the correct drop-down menu before selecting the option at index 2.
Set dropOptions = HTMLDoc.getElementsByTagName("select")
For Each op In dropOptions
If op.getAttribute("name") = "tilastojakso" Then
op.Focus
op.selectedIndex = 2
Exit For
End If
Next op
And here is the HTML of the relevant dropdown:
<tr>
<td>Statistical period</td>
<td colspan="2"><select name="tilastojakso" >
<option value="2016-07">2016-07 (deadline 12.08.2016)</option>
<option value="2016-06">2016-06 (deadline 14.07.2016)</option>
<option value="2016-05">2016-05 (deadline 14.06.2016)</option>
<option value="2016-04">2016-04 (deadline 16.05.2016)</option>
<option value="2016-03">2016-03 (deadline 14.04.2016)</option>
<option value="2016-02">2016-02 (deadline 14.03.2016)</option>
<option value="2016-01">2016-01 (deadline 12.02.2016)</option>
<option value="2015-12">2015-12 (deadline 18.01.2016)</option>
<option value="2015-11">2015-11 (deadline 14.12.2015)</option>
<option value="2015-10">2015-10 (deadline 13.11.2015)</option>
<option value="2015-09">2015-09 (deadline 14.10.2015)</option>
<option value="2015-08">2015-08 (deadline 14.09.2015)</option>
<option value="2015-07">2015-07 (deadline 14.08.2015)</option>
<option value="2015-06">2015-06 (deadline 14.07.2015)</option>
<option value="2015-05">2015-05 (deadline 12.06.2015)</option>
<option value="2015-04">2015-04 (deadline 18.05.2015)</option>
<option value="2015-03">2015-03 (deadline 16.04.2015)</option>
</select></td>
The problem:
I want to select from the dropdown based on option value, rather than index. Let's say I have a variable called "period" which contains the string "2016-04". I want the script to pick the option with a value that matches the string stored in "period". I haven't been able to find an answer that I can understand.
Change
op.Focus
op.selectedIndex = 2
to
op.Value = period
Answer by #Jordan moved to community wiki from comments
You can make use of the below code to select the options by value
Set dropOptions = HTMLDoc.getElementsByTagName("select")
For Each o In dropOptions.Options
If o.Value = "2016-04" Then
o.Selected = True
Exit For
End If
Next

Missing value from variable

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