isDisplayed方法
这个方法是用来检查元素是否在页面上,返回值为布尔值
判断某个元素是否存在页面上(这里的存在不是肉眼看到的存在,而是html代码的存在。
某些情况元素的visibility为hidden或者display属性为none,我们在页面看不到但是实际是存在页面的一些元素)
应用:
package com.ming.Selenium;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class IsDisplay {
public static void main(String[] args) {
ChromeDriver chromeDriver = new ChromeDriver();
chromeDriver.manage().window().maximize();
chromeDriver.get("https://www.baidu.com/");
//判断元素是否存在
Boolean s = chromeDriver.findElement(By.xpath("//*[@id=\"kw\"]")).isDisplayed();
System.out.println(s);
chromeDriver.quit();
}
}
isSelect方法
这个方法是用来查看某个元素是否被选中
应用:
package com.ming.Selenium;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
public class IsSelect {
public static void main(String[] args) {
ChromeDriver chromeDriver = new ChromeDriver();
chromeDriver.manage().window().maximize();
chromeDriver.get("https://the-internet.herokuapp.com/checkboxes");
//查看是否选中
Boolean s1 = chromeDriver.findElement(By.xpath("//*[@id=\"checkboxes\"]/input[1]")).isSelected();
Boolean s2 = chromeDriver.findElement(By.xpath("//*[@id=\"checkboxes\"]/input[2]")).isSelected();
System.out.println("s1 = "+ s1 +" s2 = "+s2);
//chromeDriver.quit();
}
}
结果如图所示:
isEnable方法
该方法用于查看元素是否被禁用,比如有一个按钮在某中情况下置灰不可点击,可以用is_enable 来判断
应用:
package com.ming.Selenium;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
public class IsEnable {
public static void main(String[] args) {
ChromeDriver driver = new ChromeDriver();
driver.manage().window().maximize();
//navigates to url
driver.get("https://www.baidu.com/");
//
boolean value = driver.findElement(By.xpath("//*[@id=\"su\"]")).isEnabled();
System.out.println("value =" +value);
driver.quit();
}
}