XPath : Get attribute value if it is blank or not - xpath

I have an html data like below
<select>
<option value="1" disabled="disabled">1</option>
<option value="2" disabled="disabled">2</option>
<option value="3" disabled>3</option>
<option value="2" disabled="disabled">2</option>
<option value="3" disabled>3</option>
</select>
I'm using this xpath selector
//select/option///#disabled
This selector returns 3 disabled text like this:
disabled
disabled
disabled
But I want is selecting all options disabled value what ever they has a value or not, some thing like this
disabled
disabled
NULL
disabled
NULL
or some thing like this is also acceptable
true
true
false
true
false
is this possible by xpath selectors?

Try to use this one:
//select/option/not(#disabled = '')
Result should be
true
true
false
true
false

Related

Submit a disabled selected option in a Codeception Functional test

I'm trying to test my backend by passing invalid options to it. I have a select item like so:
<label for="title">Title</label>
<select id="title" name="title" required="required">
<option value="" disabled="" selected="selected">Select one</option>
<option value="Mr">Mr</option>
<option value="Ms">Ms</option>
<option value="Mrs">Mrs</option>
<option value="Miss">Miss</option>
<option value="Dr">Dr</option>
<option value="Sir">Sir</option>
<option value="Prof">Prof</option>
</select>
The user must select an option, but there's no sensible default for a person's title so "Select one" is displayed, but can't be reselected once another option has been chosen.
However, in the Functional test
If I don't select anything, Mr (the first non-disabled option) gets submitted.
If I try to explicitly set Select one with the following code:
$I->selectOption('title', 'Select one');
I get a message saying:
[InvalidArgumentException] Input "title" cannot take "" as a value (possible values: "Mr", "Ms", "Mrs", "Miss", "Dr", "Sir", "Prof").
How can I get a Codeception Functional test to submit a disabled, selected <option/>?

How to select a default option using thymeleaf

I have a drop down like following,
<select id="role" class="form-control" th:field="*{role}" >
<option th:field="*{role}" value="USER">USER</option>
<option selected th:field="*{role}" value="ADMIN" >ADMIN</option>
<option th:field="*{role}" value="SUPERUSER">SUPERUSER</option>
</select>
I want default option as ADMIN. For that i added selected inside option tag. but default option still pointing to USER. Can anyone help me o solve this. Thanks
Set the value of "role" to "ADMIN" in your controller (in the java), before the page is rendered.
Also, you don't need all those extra th:field attributes. th:field should only appear on the select.
<select id="role" class="form-control" th:field="*{role}" >
<option value="USER">USER</option>
<option value="ADMIN" >ADMIN</option>
<option value="SUPERUSER">SUPERUSER</option>
</select>
I did something like following to get it working.
<script th:inline="javascript">
/*<![CDATA[*/
window.addEventListener("DOMContentLoaded", function() {
var optionSelected = [[${product.role}]];
if(optionSelected == null){
$("#role").val("ADMIN");
}
});
/*]]>*/
</script>
and removed the selected attribute from the option.

Select input ,selected Text codeigniter

I have form with input select in the aplication/view codeigniter
and the form is submited by codeigniter, no ajax.
<select name="sel_options">
<option value="1">hi</option>
<option value="2" selected>bye</option>
</select>
in my aplication/controller
$this->input->post("sel_options");
result:
2
but i need the text ("bye");
When you post a value, it's going to use the value from the input. So in your case:
<select name="sel_options">
<option value="1">hi</option>
<option value="<!-- I'm Submitting this value -->" selected>bye</option>
</select>
The value for the second option is 2 (<option value="2" ... />) if you wanted to pull in the value from the html ('bye'), you'd have to set the value to be bye.
<option value="bye" selected>bye</option>

Selecting second Dropdown Value Watir-Webdriver

Design:
First Dropdown:
<select id="MainContent_drpVehicleType" style="width:175px;" name="ctl00$MainContent$drpVehicleType">
<option value="">- SELECT -</option>
<option value="1" title="AUTO">AUTO</option>
<option value="2" title="HD">HD</option>
<option value="3" title="MARINE">MARINE</option>
</select>
Second Drop Down:
<select id="MainContent_drpMake" style="width:175px;" name="ctl00$MainContent$drpVehicleType">
<option value="1" title="ACURA">ACURA</option>
<option value="2" title="ALFA ROMEO">ALFA ROMEO</option>
<option value="74" title="ALLIS CHALMERS LIFT TRUCK">ALLIS CHALMERS LIFT TRUCK</option>
<option value="75" title="ALLIS CHALMERS TRACTOR">ALLIS CHALMERS TRACTOR</option>
<option value="4" title="AMERICAN MOTORS">AMERICAN MOTORS</option
</select>
Code used for execution:
b.select_list(:id, "MainContent_drpVehicleType").select("AUTO")
b.select_list(:id, "MainContent_drpMake").select("ACURA")
and also tried
`b.select_list(:id, "MainContent_drpMake").wait_until_present.option(:text, 'ACURA')`
**What my Problem is able to select "AUTO" from first dropdown
and not able to select "ACURA" from second dropdown
Error While Executing:
C:/Ruby193/menu.rb:23:in `<main>':
C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.2/lib/watir-webdriver/el
ements/select.rb:218:in `no_value_found': "ACURA" not found in select list (Wati
r::Exception::NoValueFoundException)
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.2/lib/watir
-webdriver/elements/select.rb:152:in `rescue in select_by_string'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.2/lib/watir
-webdriver/elements/select.rb:149:in `select_by_string'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.2/lib/watir
-webdriver/elements/select.rb:131:in `select_by'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.2/lib/watir
-webdriver/elements/select.rb:64:in `select'
from C:/Ruby193/menu.rb:23:in `<main>'**
It sounds like your second dropdown is populated after the first dropdown value is selected. In that case, you need to wait for the second dropdown list to be populated (rather than for the second dropdown to appear).
You can wait for the specific option to appear and then set it by doing:
#The option element that you want:
option = b.select_list(:id, "MainContent_drpMake").option(:text => "ACURA")
#Wait for the option to appear
option.wait_until_present
#Set the option
option.select
Or if you want to do it on one line, you can use when_present:
b.select_list(:id, "MainContent_drpMake").option(:text => "ACURA").when_present.select

display selected item from dropdown onchange function into html label through javascript , please help how to it

<form method="post">
<select name="users" id="users" onchange="showUser(this.value)" value="">
<option value="">Select a person:</option>
<option value="1" onchange="copy();">tcs</option>
<option value="2" onchange="copy();">wipro</option>
<option value="3" onchange="copy();">Hcl</option>
<option value="4" onchange="copy();">krystal kones</option>
</select>
</form>
I have this drop down i want that when the value is changed like tcs,wipro or hcl then that value should shown in html label
Try putting the onChange attribute in the select tag.
Example: http://jsfiddle.net/r6Fus/
HTML:
<div id="label"></div>
<select id="mySelect" onchange="copy();">
<option value="">Select a person:</option>
<option value="tcs" >tcs</option>
<option value="wipro" >wipro</option>
<option value="Hcl" >Hcl</option>
<option value="krystal kones" >krystal kones</option>
</select>
Javascript:
function copy() {
document.getElementById("label").innerHTML = document.getElementById("mySelect").value
}
Otherwise you could use jQuery.
First of all, the onchange event is for the select element, not the option elements. Those don't actually change. Also, you have two JavaScript functions. showUser() and copy(). But you describe only one piece of functionality. What do these two functions do?
As for showing the text in the label, here's one way to do it (using jQuery, because everybody does):
$(document).ready(function() {
$('#users').change(function() {
$('#myLabel').text($(this).val());
});
});
What this is basically doing is:
Wait until the DOM is loaded and ready.
Bind a function to the change event of the specified select element.
The function contains one line, which sets the text of the specified label to the value of the select.

Resources