Webbrowser control:
Dim element As HtmlElement =
WebBrowser1.Document.GetElementsByTagName("select").Cast(Of HtmlElement).First(Function(el) el.GetAttribute("name") = "package_id")
element.GetElementsByTagName("option").Cast(Of HtmlElement).First(Function(el) el.InnerText = "UNL").SetAttribute("selected", "selected")
How to do this in Awesomium?
Tried this:
WebControl1.ExecuteJavascript("$('#country').value('NL');")
WebControl1.ExecuteJavascript("document.getElementById('country').selectedIndex = NL")
Doesn't work. Can anyone give me a little help here?
Your last one looks like it should work, except you can't set a text value to selected index...it only takes integers, so you'd need to know NL's index to select it that way. If you don't know the index, you will have to either loop through the select options and find it, or try using queryselector. If you know the value of the select option, use this:
WebControl1.ExecuteJavascript("document.querySelector('option[value='NL']').selected = true;")
Note that the code above is searching the values, not the text...and also the 'NL' part must be an exact match to what's in the select, including single vs. double quotes. For example:
<option value="CA">California</option> would need queryselector('option[value="CA"]'])
and
<option value='CA'>California</option> would need queryselector('option[value='CA']'])
Related
I am having trouble using selecting from this select element.
<select name="vehicle_attrs[position_count]" class="mb1"><option>Position / Quantity</option><option>Front</option><option>Rear</option></select>
I have tried
select('Front', :from=>'mb1')
select('Front', :from=>'vehicle_attrs[position_count]')
select('Front', :from=>'vehicle_attrs[1]')
All of them result in a can not find selection box error
I've never liked how restrictive Capybara's concept of a 'locator' is (i.e. must have a name/id/label), but if you dig into the source code, those helpful methods like select, click_on, and fill_in are just wrappers for find and some native method of Element, which takes arbitrary CSS, and works in almost all situations. In this case, you could use:
find('[name="vehicle_attrs[position_count]"]').find('option', text: 'Front').select_option
Since dropdowns often have multiple similar options, where one is a substring of the other, you might consider using an exact string match, like
find('[name="vehicle_attrs[position_count]"]').find('option', text: /\AFront\z/).select_option
From the docs for select - https://www.rubydoc.info/github/teamcapybara/capybara/Capybara/Node/Actions#select-instance_method - we can see that the from option takes "The id, Capybara.test_id atrtribute, name or label of the select box".
Neither 'mb1' or 'vehicle_attrs[1]' are any of those so they would be expected to fail.
'vehicle_attrs[position_count]' is the name so assuming the box is actually visible on the page (not replaced with a JS driven select widget, etc), that should work. If it doesn't, then edit your question and add the full exact error message you get when trying to use it. Of course if there is only one select box on the page with an option of 'Front' then you don't need to specify the from option at all and can just do
select 'Front'
Is there a way to use Watir to select an item from a dro pdown list by item number? That is, when using SelectList, does option() take anything else besides value and text?
I'm trying to run a process on a webpage where I select the first item on a drop down list, do an operation, go to the second item, do the operation again, etc. And this drop down list has over 700 options!
Here's what the HTML looks like:
<SELECT NAME="sl" SIZE="1">
<option value=""> </OPTION>
<option value="abq">Abaza</option>
<option value="abk">Abkhazian</option>
...
<option value="zun">Zuni</option>
</SELECT>
The SelectList documentation suggests something like:
b.select_list(:name => 'sl').select_value("abq")
The question is then how do I move to the next value? And the next 700? Is there a way to select by item number? Or extract the values into an array and then cycle through them?
Thanks!
When you want to select an option element by anything other than text or value, you can locate and select the element directly. For example, equivalent to using the select_value method is:
b.select_list(:name => 'sl').option(value: 'abq').click
You can use the usual locators. To select the second option, you can use the :index locator (note that it is 0-based):
b.select_list(:name => 'sl').option(index: 1).click
Note that given you want to iterate through each option, it might make more sense to use one of the Enumerable methods to do the loop. This can eliminate the need to worry about the index. However, may not work depending on what the other actions you are planning to take.
b.select_list(:name => 'sl').options.each do |option|
option.click
# Do other actions
end
Im beating my head against the wall - someone please help.
Using IE webbrowser i simply used the following.
collection as htmlcollection = htmldoc.getelementbytagname("select")
for each ele in collection
dim options = ele.children
'some if thens to match the option to the option i need selected
ele.setattribute("value",optionselectedbyuser)
All this worked great, it set the value and selected it in the webpage dropdownbox. Now that the website requires Firefox i have to redo my coding and the drop down boxes are killing me this is what i have tried
dim collection as gecko.geckoelementcollection =htmldoc.getelementbytagname("select")
for each ele as gecko.element in collection
'some if thens to make sure im in the correct dropdown control
for each child in ele.childnodes
'using this to obtain the options of the dropdownbox and make sure it matches what user has selected, if it does i assign the element that value
if child.textcontent.tostring.toupper = inputfromusertoselect.toupper then
ele.setattribute("value",child.nodevalue("value"))
end if
next
next
After assigning the value, i reread the outerhtml code and the value i set is now in the html but on the webpage the actual item isnt selected. I also notice in the outerhtml there is an option tag with its own value. Im wondering if this is where i need to set the value but i cant seem how to assign the value of the options. see outer html
<select selected="selected" value="Detached" id="GarageType" name="GarageType" class="required"><option value="">-- Select Garage Type --</option>
<option id="GarageTypeNone" value="None">None</option>
<option id="GarageTypeAttached" value="Attached">Attached</option>
<option id="GarageTypeDetached" value="Detached">Detached</option>
<option id="GarageTypeCarport" value="Carport">Carport</option>
<option id="GarageTypeBuiltIn" value="Built In">Built In</option></select>
as you can see in html code, i have already set the value to "Detached" and by trouble shooting i set the selected =selected. but to no avail it still doenst change value on page. What am i doing wrong with geckofx!!!HELLLP
Tested in Visual Studio 2013 - VB.NET:
Dim el1 As Gecko.DOM.GeckoSelectElement = _
GeckoWebBrowser1.Document.GetElementsByName("GarageType")(0)
el1.Options.item(<NUMBER>).Selected = True
Sorry, code only answer since I do not speak English.
I'm trying to use the django localflavor widget USStateSelect() in a form, but I want the widget to have Nebraska selected by default. The Widget only accepts an attrs arg, so I'm trying to understand what attributes I have to set to get the desired result. Here's what I've got in my forms.py:
state = forms.CharField(widget=USStateSelect(attrs={'value':'NE'}))
This is the docs for the HTML select element: http://www.w3.org/html/wg/drafts/html/CR/forms.html#the-select-element
This is the docs for localflavor: https://django-localflavor.readthedocs.org/en/latest/_modules/localflavor/us/forms/
<option value="NE" selected>Nebraska</option>
This is what I need to have in my html, but I can't figure out what the attrs dict needs to contain to achieve this result. I've tried adding 'selected':'selected' and 'class':'selected' to the dict, but that's not doing it.
I've seen a number of people asking how to add an empty option, but no one seems to want to make it default to a specific state. Any ideas are welcome.
Thanks,
Anthony
You can set the initial value like so:
state = forms.CharField(widget=USStateSelect(), initial='NE')
or you can set it when you instantiate the form:
form = ExampleForm(initial={'state': 'NE'})
I've got a select with two 'special' options, a bit like this:
<select>
<option value="????">Choose one:</option>
<option value="1">option1</option>
<option value="2">option2</option>
....
<option value="????">Free input</option>
</select>
When the user selects nothing, I should ignore the input. When the user selects 'free input', the value from a corresponding textbox is used.
I was wondering, which values would you give those options? I figured I should be able to use no value for the first, because that's what it is: no value. But the last option, should I use -1, 0, or something different?
I would use something that will be understandable to the next developer who will be reading the code. Therefore something like 'freeInput' or 'textFieldLookupValue' could be appropriate
If the rest of your options has numeric values, choose non-numeric values with "speaking values" for the special options.
This way the server logic can decide with a simple IsNumeric() call what to do, and you are free to add other special options later on.
Why not use "free" and "none". You are not restricted to integer values, so using descriptive values would improve comprehensibility.
How are you processing your data?
In C#, I will define a constant to represent the ID, and use this to identify it.
public string CONSTANT_OPTION_FreeInput = "-1";
// Test for value in a different method:
if (selectedValue == CONSTANT_OPTION_FreeInput) {
// Do Stuff
}
You can do something similar in other languages as well.
Whatever value you use, just make sure that it will be easily understood by yourself and any other developer looking at the code in the future.
On the first one, you really don't even need the value attribute present. It'll come up as not being set on the server side regardless.
With regards to the free input choice, I'd go simply with free_input.
Of course, it's all up to you. Just choose something that is meaningful and relates to its purpose. ;-)
Make the first option an option group instead and then use no value for your "Free input". An option group is not selectable.