如何获得具有On&的On / Off开关的当前值。关闭标签?
此开关的HTML代码如下 -
<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> </label>
<span class="switch-right">C</span>
我尝试了以下代码,但总是在其他情况下 -
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" );
}
答案 0 :(得分:0)
你应该尝试如下: -
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" );
}
已修改: - 找到checkbox
使用它的data-bind
属性,如下所示: -
WebElement el = driver.findElement(By.xpath('//input[@data-bind="switch: DegreeSetting, switchOptions: { onLabel: \'F\', offLabel: \'C\' }"]'));
或
WebElement el = driver.findElement(By.xpath("//input[contains(@data-bind,'switch: DegreeSetting')]"));
希望它会帮助你...... :)
答案 1 :(得分:-1)
这应该有所帮助:https://stackoverflow.com/a/11599728/6548154
对于现代浏览器:
var checkedValue = document.querySelector('.messageCheckbox:checked').value;
使用jQuery:
var checkedValue = $('.messageCheckbox:checked').val();
没有jQuery的纯javascript:
var checkedValue = null;
var inputElements = document.getElementsByClassName('messageCheckbox');
for(var i=0; inputElements[i]; ++i){
if(inputElements[i].checked){
checkedValue = inputElements[i].value;
break;
}
}