1

How to get current value of a On/Off switch having On & Off labels?

The HTML code for this switch is as follow-

<div class="make-switch has-switch">
<div class="switch-on switch-animate">
<input type="checkbox" data-bind="switch: DegreeSetting, switchOptions: { onLabel: 'F', offLabel: 'C' }">
<span class="switch-left">F</span>
<label>&#160;</label>
<span class="switch-right">C</span>

I tried the following code, but is always goes in else case-

boolean isChecked = "true".equals(driver.findElement (By.xpath("given xpath of class="switch-on switch-animate")).getAttribute("Checked"));
if (isChecked){
 Log.info("Current F/C setting is : F" );
}else{
 Log.info("Current F/C setting is : C" );
}
Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
J.Doe
  • 11
  • 2
  • Possible duplicate of [Selenium checkbox attribute "checked"](http://stackoverflow.com/questions/8187772/selenium-checkbox-attribute-checked) – JeffC Jul 07 '16 at 03:17

2 Answers2

0

You should try as below :-

WebElement el = driver.findElement(By.xpath("//div[contains(@class, 'switch-on switch-animate')]/input[@type='checkbox']"));

boolean isChecked = el.isSelected();

if (isChecked)
{
  Log.info("Current F/C setting is : F" );
}
else
{
  Log.info("Current F/C setting is : C" );
}

Edited :- to locate checkbox using it's attribute data-bind use as below :-

WebElement el = driver.findElement(By.xpath('//input[@data-bind="switch: DegreeSetting, switchOptions: { onLabel: \'F\', offLabel: \'C\' }"]'));

or

WebElement el = driver.findElement(By.xpath("//input[contains(@data-bind,'switch: DegreeSetting')]"));

Hope it will help you...:)

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • Using above code, it gives error - Unable to locate element. – J.Doe Jul 07 '16 at 08:07
  • @J.Doe paste this `$x("//div[contains(@class, 'switch-on switch-animate')]/input[@type='checkbox']")` at your browser console where that check box is present and let me know this returns that correct check box which you want or not.. – Saurabh Gaur Jul 07 '16 at 08:14
  • I am using eclipse to develop this code and it does not accept $x. – J.Doe Jul 07 '16 at 09:58
  • It returns as [ ]. I am not understanding the meaning of it. – J.Doe Jul 12 '16 at 06:48
  • @J.Doe it means provided `xPath` does not return element which you want. it returns empty array.. you need to provide correct xPath of checkbox element to the above provided code which locate to that checkbox element which you want.. – Saurabh Gaur Jul 12 '16 at 06:53
  • @J.Doe as your provided HTML xpath is correct but we can not be sure without seeing your page...could you share your website url where you want to automate this... – Saurabh Gaur Jul 12 '16 at 06:55
  • @J.Doe one thing more, make sure this element is not inside any frame.. – Saurabh Gaur Jul 12 '16 at 06:56
  • Can we use data-bind to locate this element? – J.Doe Jul 12 '16 at 08:22
  • @J.Doe use this `//input[contains(@data-bind,'switch: DegreeSetting')]` – Saurabh Gaur Jul 12 '16 at 08:25
  • @J.Doe or this `//input[@data-bind="switch: DegreeSetting, switchOptions: { onLabel: \'F\', offLabel: \'C\' }"]`...see updated answer.. – Saurabh Gaur Jul 12 '16 at 08:34
-1

This should help: https://stackoverflow.com/a/11599728/6548154

For modern browsers:

var checkedValue = document.querySelector('.messageCheckbox:checked').value;

By using jQuery:

var checkedValue = $('.messageCheckbox:checked').val();

Pure javascript without jQuery:

var checkedValue = null; 
var inputElements = document.getElementsByClassName('messageCheckbox');
for(var i=0; inputElements[i]; ++i){
  if(inputElements[i].checked){
       checkedValue = inputElements[i].value;
       break;
  }
}
Community
  • 1
  • 1
A_A
  • 1,832
  • 2
  • 11
  • 17