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

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()

Related

Regular Expression for Extracting values from dropdown in Jmeter

To start with - I am new to writing regular expressions.
I have two dropdowns as FromCity and ToCity which ate HTML options (as dropdowns). However both are having same left right boundary due to which not able to extract the specific values of a drop down at runtime.
e.g.
FROM CITY DROPDOWN
<select name="fromPort" class="form-inline">
<option value="Paris">Paris</option>
<option value="Philadelphia">Philadelphia</option>
<option value="Boston">Boston</option>
<option value="Portland">Portland</option>
<option value="San Diego">San Diego</option>
<option value="Mexico City">Mexico City</option>
<option value="São Paolo">São Paolo</option>
</select>
TO CITY DROPDOWN
<select name="toPort" class="form-inline">
<option value="Buenos Aires">Buenos Aires</option>
<option value="Rome">Rome</option>
<option value="London">London</option>
<option value="Berlin">Berlin</option>
<option value="New York">New York</option>
<option value="Dublin">Dublin</option>
<option value="Cairo">Cairo</option>
</select>
I can fetch the city names with - <option value="(.*?)"> but not able to distinguish which value is for which dropdown.
Is there a better way to handle this using regular expression ?
Using regular expressions for parsing HTML is not the best option, I would rather suggest using CSS Selector Extractor instead
This way you will be able to get "from" city names as select[name=fromPort] option and "to" city names as select[name=toPort] option
Demo:
More information:
CSS Selectors Reference
How to Use the CSS/JQuery Extractor in JMeter

Find which option is selected

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()

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());
});

how to make a select box which has a particular value preselected using grails2.0

I have the following line in my gsp
<g:select name="platform" from="['Date','Time','Place','Calendar']"/>
I want the Time option to be automatically selected while loading the page.
<select>
<option value="Date">Date</option>
<option value="Time" selected="selected">Time</option>
<option value="Place">Place</option>
<option value="Calendar">Calendar</option>
</select>
This is what i need when my gsp page got rendered into html.
Please help me
Thanks in advance
You can do it by setting the g:select value
<g:select name="platform" from="['Date','Time','Place','Calendar']" value="'Time'"/>
but it would be better to bind it to an model.

Html select dropdown list - how to choose the selected value from server code

I have a select list say of states in the country, which i have in a helper to include easily in any form. (removing most options to make it brief).
I have the value of the current selection stored in the database say "CA". How would i set selected="true" to option CA before rendering the list to the user?
#helper StateSelect(string name = "State")
{
<select name="#name" id="#name" class="required">
<option value="">-- Select -- </option>
<option value="AK">Alaska</option>
<option value="AL">Alabama</option>
<option value="AR">Arkansas</option>
<option value="AZ">Arizona</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="VA">Virginia</option>
<option value="VT">Vermont</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select>
}
As Darin Dimitrov says, the built-in stuff would be better. However, if you do need to, I think you have a few options:
Add code like this to every line:
<option value="CA" #(name == "CT" ? "selected=selected" : "")> Connecticut</option>
Just re-add the selected item to the top of the list
This keeps the code cleaner, the selected option is just repeated at the top (and selected there), before the entire list

Resources