Find which option is selected - xpath

I have this piece of code:
<select id="customizableonly" class=" select" name="product[customizableonly]">
<option value="1">Yes</option>
<option selected="selected" value="0">No</option>
</select>
And I need an xpath string that will find this <select id="customizableonly"...> tag and see which of its 2 options has the attribute selected="selected". Therefore, return the value of the "selected" option (Yes or No). If someone can give a quick solution - I'd appreciate that a lot!
I've got this far //select[#id='customizable'] for acquiring the main tag.

If I get it right, all you have to do is
//select[#id='customizableonly']/option[#selected='selected']/text()

Related

Selenium does not find element on page (ruby, watir)

I can not locate the element on the page in any way, and I would not like to use xpath because the layout of the page can be changed later and complex to maintain.
I tried the following ways:
Page Structure
<select class="form-control errorClass1" id="idPage:idForm10:adhPaymentMethodId" name="adhPaymentMethodId">
<option value="0">Escolha um</option>
<option value="CreditCard">Cartão de Crédito</option>
<option value="Boleto">Boleto</option>
</select>
Attempts
#browser.select_list(:name, "adhPaymentMethodId").click
#browser.option(:text, "Boleto").click
#browser.select_list(:id, "idPage:idForm10:adhPaymentMethodId").click
#browser.option(:text, "Boleto").click
#browser.select_list(:id, "idPage:idForm10:adhPaymentMethodId").option.wait_until_present
sleep 1
#browser.option(:value, "Boleto").click
Try this
#browser.select_list(:name, "adhPaymentMethodId").select("Boleto")
If it's not working, let me know what error it is throwing

How to get XPATH to select blank value from Drop down box

I have web page and from where i am trying to select the XPATH from a drop down box which dont have any value.
<select name="selectOption" id="selectOption">
<option value="">Please select</option>
<option value="Op1">Op1</option>
<option selected="selected" value="Op2">Op2</option>
<option value="Op3">Op3</option>
</select>
I am trying to select the "Please Select" option as below by XPATH,
//*[#id="selectOption"]/option[1]
but i am not geeting the Please Select.Here Please select has value as blank.
And Suggestion approach must be appreciated.Thanks
Try using Select.selectByVisibleText()

Change dropdowns with same .class name

I have three dropdowns with same class name:
<select class="MyClass">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<select class="MyClass">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<select class="MyClass">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
I want to change any of these dropdown values based on any other dropdrop selected. If I select option two from second dropdown - I want 1st and 3rd dropdown values to be two. All these dropdown should change values no matter which one I select.
Thanks.
You will need JavaScript to do this. Here is an code example using jQuery:
$('.MyClass').on('change',function(){
$('.MyClass').val( $(this).val() );
});
This will add an EventListeneer for the change Element and update all DropDowns to the value of the changed one.
http://jsfiddle.net/CWP7Q/
you can use jQuery or normal JS, but my example contains Jquery, which will give you an idea. Please check the example in Jsfiddle.
The example binds a change event to the class name of the three selectors:
$(".MyClass").change(function(){
$(".MyClass").val($(this).val());
});
and Voila, every selector gets changed.
Somehting like (with jQuery)
$('.MyClass').change(function() {
$('.MyClass').val($(this).val());
});

jQuery Prev() Question

Sorry guys, I should have posted the script directly without cleaning it. Please check the updated script, it should now clear things up. Thanks!
Consider the following JavaScript :
var selected = 0;
var options = 0;
$('.newListSelected').each(function() {
selected = $(this).children('.selectedTxt');
selected = selected.text();
/* Everything before this line works completely fine */
options = $(this).prev();
options.find('option[value=' +selected+ ']').attr('selected', 'selected');
}).remove();
And HTML :
<select name="type[]" style="display: none;">
<optgroup>
<option value="none">Select</option>
</optgroup>
<optgroup label="First Group">
<option value="1">One</option>
<option value="2">Two</option>
</optgroup>
<optgroup label="Second Group">
<option value="10">Ten</option>
<option value="20">Twenty</option>
</optgroup>
</select>
<div class="newListSelected">
<div class="selectedTxt">20</div>
<ul class="newList">
<!-- Other stuff here -->
</ul>
</div>
What I'm trying to do actually is adding the selected attribute to the corresponding select option that has the same value as the text in .selectedTxt. In the code above, it should add selected="selected" to <option value="20">Twenty</option>.
However its not performing as expected, I also tried adding alert(option); below prev(); but it didn't output anything useful.
Thanks.
The Javascript works fine, as can be seen here: http://jsfiddle.net/4HsXp/
If you need to be able to select multiple items in a select element, you need to set the multiple and size attribute, like this:
<select multiple="multiple" size="2">
<option>1</option>
<option>2</option>
</select>
See: http://reference.sitepoint.com/html/select
Edit:
Attaching console.log statements to the new code still does not any problem. Running it on jsfiddle gave me the correct output:
jQuery(select)
Original Value: none
New Value: 20
I'm not sure what you are trying to do, but why couldn't you just select all option elements with proper selectors? If you want to select only some options, then you have to add extra selector attrbiutes.
$('select option').each(function() {
$(this).attr('selected', 'selected');
});

dropdown menu question, may be simple may not be

I have a webpage containing input tags like the one below eg.
<input value='Cut' name='container_cut_button' type='submit/>
<input value='Copy' name='container_copy_button' type='submit/>
I want to put these into a dropdown, I was hoping it would be something simple like
<select onchange='submit()'>
<option name='container_cut_button'>Cut</option>
<option name='container_copy_button'>Copy</option>
</select>
but no such luck,
Anyone have any ideas about how this could be done?
Thanks in advance
Use the "value" attribute of the options rather than their name.
<select name="action">
<option value="cut_item">Cut</option>
<option value="save_item">Save</option>
</select>
On the server, you'll check the value of the variable "action." It will be either "cut_item" or "save_item".
I called a javascript function in the select tag ie. onchange="exec(value)", the function gets the select id and inserts the proper name attribute based on it value.
function exec(val){
if(val=='cut'){
document.getElementById("action").setAttribute("name", "cut")
}
}
This worked out ok

Resources