官术网_书友最值得收藏!

The isSelected() method

The isSelected method returns a boolean value if an element is selected on the web page and can be executed only on a radio button, options in select, and checkbox WebElements. When executed on other elements, it will return false. The API syntax for the isSelected() method is as follows:

boolean isSelected()

The preceding method returns a Boolean value specifying whether the target element is selected on the web page. The following is the code to verify whether the Search box is selected on a search page:

@Test
public void elementStateExample() {
WebElement searchBox = driver.findElement(By.name("q"));
System.out.println("Search box is selected: "
+ searchBox.isSelected());
}

The preceding code uses the isSelected() method. It returns false for the Search box, because this is not a radio button, options in select, or a checkbox. The preceding code returns false for the Search box:

Search box is selected: false
To select a Checkbox or Radio button, we need to call the WebElement.click() method, which toggles the state of the element. We can use the  isSelected() method to see whether it's selected.
主站蜘蛛池模板: 隆尧县| 盐山县| 南宁市| 洪湖市| 庆安县| 马山县| 通化县| 长白| 阳春市| 睢宁县| 同仁县| 阜南县| 镇康县| 汽车| 苍梧县| 太和县| 潮州市| 曲水县| 宁晋县| 襄汾县| 金溪县| 甘南县| 巴彦淖尔市| 安塞县| 白沙| 崇礼县| 洛南县| 工布江达县| 读书| 运城市| 太仓市| 尉犁县| 钦州市| 增城市| 蓬莱市| 平阳县| 兴和县| 旺苍县| 囊谦县| 班戈县| 额济纳旗|