如何获取带有On&Off标签的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" );
}
最佳答案
您应该尝试以下方法:
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')]"));
希望对您有帮助... :)