Selenium(一)10.isEnable() 和 isDisplayed() 和 isSelected()

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();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值