前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SeleniumWebDriver处理复选框CheckBox和单选按钮RadioButton

SeleniumWebDriver处理复选框CheckBox和单选按钮RadioButton

作者头像
软测小生
发布2019-09-10 16:13:06
3.4K0
发布2019-09-10 16:13:06
举报
文章被收录于专栏:软测小生

该文章主要讲解如何识别复选框CheckBox和单选按钮RadioButton

  • 单选按钮RadioButton
  • 复选框CheckBox
  • 查找元素异常情况汇总

单选按钮RadioButton

单选按钮也可以通过Click()方法打开

使用网页http://demo.guru99.com/test/radio.html作为练习,如下: 使用radio1.click() 切换到Option1单选按钮; 使用radio2.click() 切换到Option2单选按钮,取消选中Option1 ; 代码如下图所示:

在这里插入图片描述

复选框CheckBox

使用click()方法切换复选框的状态:开/关。 如下的代码是使用账户名和密码登陆百度网址,其中可见到下次自动登陆的复选框。

在这里插入图片描述

代码语言:javascript
复制
WebElement memberPass1; 
memberPass1 = driver.findElement(By.xpath("//*[@id='TANGRAM__PSP_10__memberPass']"));
memberPass.click();
System.out.println("是否选中:" + memberPass.isSelected());

其输出为:"是否选中:False"

isSelected() 方法的作用是:判断复选框是否被勾选。

这里有另外一个例子:Demo主页http://demo.guru99.com/test/radio.html

在这里插入图片描述

完整代码如下:

代码语言:javascript
复制
import org.openqa.selenium.By;        
import org.openqa.selenium.WebDriver;        
import org.openqa.selenium.chrome.ChromeDriver;        
import org.openqa.selenium.*;        

public class Form {                
    public static void main(String[] args) {                                    
        // 对象/变量的声明和实例化
        System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");                   
        WebDriver driver = new ChromeDriver();                    

        driver.get("http://demo.guru99.com/test/radio.html");                    
        WebElement radio1 = driver.findElement(By.id("vfb-7-1"));                            
        WebElement radio2 = driver.findElement(By.id("vfb-7-2"));                            

        //选择单选按钮Option1    
        radio1.click();            
        System.out.println("Radio Button Option 1 Selected");                    

        //Button1未被选中,Button2被选中        
        radio2.click();            
        System.out.println("Radio Button Option 2 Selected");                    

        // 选择复选框    
        WebElement option1 = driver.findElement(By.id("vfb-6-0"));                            

        // 这将切换复选框
        option1.click();            

        // 检查复选框是否已被选中         
        if (option1.isSelected()) {                    
            System.out.println("Checkbox is Toggled On");                    
        } else {            
            System.out.println("Checkbox is Toggled Off");                    
        }        

        //选择复选框并使用isSelected方法
        driver.get("http://demo.guru99.com/test/facebook.html");                    
        WebElement chkFBPersist = driver.findElement(By.id("persist_box"));                            
        for (int i=0; i<2; i++) {                                            
            chkFBPersist.click ();             
            System.out.println("Facebook Persists Checkbox Status is -  "+chkFBPersist.isSelected());                            
        }        
        //driver.close();               
    }        
}

查找元素异常情况汇总:

如果在查找元素时遇到NoSuchElementException(),这意味着在WebDriver访问该页面时,该元素不在页面中。

  1. 使用FireFox中的Firepath或Chrome中的InspectElement(F12)检查定位元素;
  2. 检查代码中使用的值与Firepath中元素的值是否相同;
  3. 有些元素的属性动态的;如果发现值不同,并且动态变化,可以考虑使用By.xpath()或By.cssSelector(),这两种方法更可靠,但语法结构更复杂一点;
  4. 另外,还有可能是等待问题,WebDriver甚至在页面完全加载之前就执行了代码,所以找不到元素,等等。
  5. 使用隐式或显式等待,在查找定位元素之前;等待详情请参考文章:Selenium三种等待

下表总结了访问上面讨论的每种类型元素的命令:

Element

命令

描述

Check Box, Radio Button

click()

用于切换元素是否选中

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-09-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 软测小生 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 单选按钮RadioButton
  • 复选框CheckBox
  • 查找元素异常情况汇总:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档