Select <TR> under HTML by VBA - html

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"

Related

Getting the value of a select element which is hidden in a multiple select element form

I am working on a form which have multiple select fields with multiple options. But only one select field is displayed while the other is hidden with display: none.
The issue right now is, I am getting the value for both the fields even though one is hidden.
Here's my two select input fields where only one is visible according to the user's choice.
<select id="entry" name="entry">
<option value="slide-in-center" >Center</option>
<option value="slide-in-right">Right</option>
</select>
<select id="exit" name="exit" style="display: none;" >
<option value="slide-out-center" >Center</option>
<option value="slide-out-right">Right</option>
</select>
After the form is submitted I get output from both the fields. Like:
{
entry: 'slide-in-center',
exit: 'slide-out-center'
}
Why am I getting this? I had selected attribute in the first option for both. And I thought it is because of this so I removed this but it is giving out the same output. What am I doing wrong here?
The select, even with display set to none will still be submitted as part of the form. I believe it used to be enough to prevent it from being submitted, but it changed.
Adding the disabled attribute to your select will prevent it from being submitted with the form.
the default value is the first option. add a blank option
var entry = document.getElementById('entry').value
console.log(entry)
var exit = document.getElementById('exit').value
console.log(exit)
var entry2 = document.getElementById('entry2').value
console.log(entry2)
var exit2 = document.getElementById('exit2').value
console.log(exit2)
<select id="entry" name="entry">
<option value="slide-in-center" >Center</option>
<option value="slide-in-right">Right</option>
</select>
<select id="exit" name="exit" style="display: none;" >
<option value="slide-out-center" >Center</option>
<option value="slide-out-right">Right</option>
</select>
<select id="entry2" name="entry">
<option value=''></option>
<option value="slide-in-center" >Center</option>
<option value="slide-in-right">Right</option>
</select>
<select id="exit2" name="exit" style="display: none;" >
<option value=''></option>
<option value="slide-out-center" >Center</option>
<option value="slide-out-right">Right</option>
</select>

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

Extract data from HTML page in R: option values in specific select elements

I'm just getting my feet wet with extracting data from a website with R. The EIA has a webpage that provides interactive access to their data, and I would like to extract the range of years for which data is available.
I would like to extract the values for the options, but only for a specific select element (named "year1") on the webpage. How can I do this?
<span id="sub3">
<label for="year">Start Year:</label>
<select name="year1" id="year" style="font-size:12px;padding:4px 2px;border:solid 1px #aacfe4;" onchange="activeB()">
<option value="2012">2012</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
<option value="2008" selected="selected">2008</option>
<option value="2007">2007</option>
<option value="2006">2006</option>
<option value="2005">2005</option>
<option value="2004">2004</option>
<option value="2003">2003</option>
<option value="2002">2002</option>
<option value="2001">2001</option>
<option value="2000">2000</option>
<option value="1999">1999</option>
<option value="1998">1998</option>
<option value="1997">1997</option>
<option value="1996">1996</option>
<option value="1995">1995</option>
<option value="1994">1994</option>
<option value="1993">1993</option>
<option value="1992">1992</option>
<option value="1991">1991</option>
<option value="1990">1990</option>
<option value="1989">1989</option>
<option value="1988">1988</option>
<option value="1987">1987</option>
<option value="1986">1986</option>
<option value="1985">1985</option>
<option value="1984">1984</option>
<option value="1983">1983</option>
<option value="1982">1982</option>
<option value="1981">1981</option>
<option value="1980">1980</option>
</select>
</span>
I've gotten as far as downloading the page and extracting all option values on the page, but am stuck with trying to extract only those options within the "year1" select element.
library(XML)
webpage <- readLines("http://www.eia.gov/cfapps/ipdbproject/IEDIndex3.cfm?tid=2&pid=2&aid=12")
htmlpage <- htmlParse(webpage, asText = TRUE)
pageoptions <- xpathSApply(htmlpage, "//option", function(u) xmlAttrs(u)["value"])
Which gives:
head(pageoptions)
value value value value value value
"regions" "2012" "2011" "2010" "2009" "2008"
As you can see, another select list included.
So, how do I get just those 2008 - 2012 values, assuming that the page structure remains constant but the date ranges available may change over time?
Thank you.
Edit
The accepted answer works with the following code:
year <- c(NA_integer_, NA_integer_)
startline <- grep(pattern = "XMLinclude.*syid=", x = webpage, value = FALSE)
year[1] <- sub(pattern = "^.*syid=(.*)&eyid.*", replacement = "\\1", x = webpage[startline])
year[2] <- sub(pattern = "^.*eyid=(.*)&form.*", replacement = "\\1", x = webpage[startline])
Profiling, there's a big difference in memory allocation, where xml_func is jdharrison's solution, url_func is hvollmeier's solution and noxml_func is a third solution that I thought of while sleeping on the problem (using grep to find the start of the select control and then a while loop to iterate through the option values until the end of select is found and pulling out values using gsub):
time alloc release dups ref src
1 0.001 0.392 0 0 .active-rstudio-document#7 wrapper_func/noxml_func
2 0.019 13.853 0 12332 .active-rstudio-document#8 wrapper_func/xml_func
3 0.001 0.000 0 129 .active-rstudio-document#9 wrapper_func/url_func
Include an additional filter on span[#id='sub3'] to narrow the search down
library(XML)
webpage <- readLines("http://www.eia.gov/cfapps/ipdbproject/IEDIndex3.cfm?tid=2&pid=2&aid=12")
htmlpage <- htmlParse(webpage, asText = TRUE)
pageoptions <- xpathSApply(htmlpage, "//span[#id='sub3']/*/option", function(u) xmlAttrs(u)["value"])
> pageoptions
value value value value value value value value value value
"2012" "2011" "2010" "2009" "2008" "2007" "2006" "2005" "2004" "2003"
value value value value value value value value value value
"2002" "2001" "2000" "1999" "1998" "1997" "1996" "1995" "1994" "1993"
value value value value value value value value value value
"1992" "1991" "1990" "1989" "1988" "1987" "1986" "1985" "1984" "1983"
value value value
"1982" "1981" "1980"
"//select[#name='year1']/option" as your xpath would also work
#Tom, even better and much more stable, instead of scraping the page download the data as a excel file and do whatever you want :-). ( see the excel link on the page? when you inspect the element you can figure out the url of the excel xls-file )
url="http://www.eia.gov/cfapps/ipdbproject/XMLinclude_3.cfm?tid=2&pid=2&pdid=&aid=12&cid=regions&syid=2008&eyid=2012&form=&defaultid=3&typeOfUnit=STDUNIT&unit=BKWH&products="
download the file and save it:
download.file(url,"eiafile.xls")

HTML Select w/ weekdays, today selected

I have a select menu with the weekdays. I would like this menu to have todays day selected. From W3Cschools, I got this simple code for getting an int for todays date:
var d = new Date();
var n = d.getDay();
This would output a 3, since it's wednesday. My problem here is to make the specific day in the list selected. Here is my code for the sropdown:
<div class="matmenu">
<select id="selectDagMat" ONCHANGE="location = this.options[this.selectedIndex].value;">
<option value="wilink/mandag">Måndag</option>
<option value="wilink/tisdag">Tisdag</option>
<option value="wilink/onsdag">Onsdag</option>
<option value="wilink/torsdag">Torsdag</option>
<option value="wilink/fredag">Fredag</option>
<option value="wilink/lordag">Lördag</option>
<option selected="selected" value="wilink/sondag">Söndag</option>
</select>
Also, an example of the page can be found here:
http://stilius.se/wilink/
Thanks in advance!
Do you mean like this: http://jsfiddle.net/xuPz2/1/ ? You get the element via getElementById and then you set the value.

how to display selected options (drop down menu) in jsp?

how to display the selected options below in jsp
<td width="75%" valign="center">
Start Date :
<input id="startDate" name="startDate" id = "startDate" type="text" size="20"><a href="javascript:NewCal('startDate','ddmmyyyy')"><img src="$
Start Time :
<select name="startTime" id = "startTime">
<option value="0">10:00</option>
<option value="1">11:00</option>
<option value="2">12:00</option>
<option value="3">13:00</option>
<option value="4">14:00</option>
<option value="5">15:00</option>
<option value="6">16:00</option>
<option value="7">17:00</option>
<option value="8">18:00</option>
<option value="9">19:00</option>
<option value="10">20:00</option>
<option value="11">21:00</option>
</select>
</td>
i thought request.getParameterValues will be valuable but it shows only something like value="something" only but not the value between both of options tag.
Any ideas? thanks in advance
Best way is using EL (expression language). Request parameters are available as implicit objects in EL. ${param.startTime} equals request.getParameter("startTime").
If you need the option's text instead of the value it's best to create an ArrayList of Strings which you can use to generate the dropdown and to look up the value:
List<String> startTimeList = new ArrayList<String>();
startTimeList.add("10:00");
startTimeList.add("11:00");
request.setAttribute("startTimeList", startTimeList);
You can use JSTL (c:forEach) to generate the list using the index (see the varStatus attribute) as your option's value:
<c:forEach items="${startTimeList}" var="startTime" varStatus="status">
<option value="${status.index}">${startTime}</option>
</c:forEach>
You can then use the value (index) to get an item from your list: ${startTimeList[param.startTime]}.