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

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.
主站蜘蛛池模板: 巫山县| 盐津县| 曲阜市| 新沂市| 平和县| 丹江口市| 都江堰市| 马山县| 简阳市| 甘孜县| 邵东县| 休宁县| 霍林郭勒市| 锡林郭勒盟| 玛沁县| 罗山县| 尤溪县| 荆门市| 沈阳市| 裕民县| 宁乡县| 安图县| 营山县| 常山县| 扶风县| 巩留县| 枞阳县| 万山特区| 嘉荫县| 嘉黎县| 保定市| 柘城县| 长海县| 龙岩市| 綦江县| 华池县| 班玛县| 松桃| 吉隆县| 甘泉县| 晋宁县|