Python Selenium 选中 CheckBox 或者 Radio, Selenium 选中 单选框 或者 复选框, How to Select CheckBox and Radio Button in Selenium WebDriver

在本教程中,我们将看到如何识别以下表单元素

 

单选按钮

也可以使用click()方法打开单选按钮。

使用http://demo.guru99.com/test/radio.html进行练习,可以看到radio1.click()切换了“ Option1”单选按钮。radio2.click()切换“ Option2”单选按钮,而未选中“ Option1”。

Python Selenium 选中 CheckBox 或者 Radio, Selenium 选中 单选框 或者 复选框, How to Select CheckBox and Radio Button in Selenium WebDriver
Python Selenium 选中 CheckBox 或者 Radio, Selenium 选中 单选框 或者 复选框, How to Select CheckBox and Radio Button in Selenium WebDriver

 

复选框

也可以使用click()方法来打开/关闭复选框。

下面的代码将两次单击Facebook的“保持登录状态”复选框,然后在打开时将结果输出为TRUE,在关闭时将结果显示为FALSE。

Python Selenium 选中 CheckBox 或者 Radio, Selenium 选中 单选框 或者 复选框, How to Select CheckBox and Radio Button in Selenium WebDriver
Python Selenium 选中 CheckBox 或者 Radio, Selenium 选中 单选框 或者 复选框, How to Select CheckBox and Radio Button in Selenium WebDriver
Python Selenium 选中 CheckBox 或者 Radio, Selenium 选中 单选框 或者 复选框, How to Select CheckBox and Radio Button in Selenium WebDriver
Python Selenium 选中 CheckBox 或者 Radio, Selenium 选中 单选框 或者 复选框, How to Select CheckBox and Radio Button in Selenium WebDriver

isSelected()方法用于知道复选框是打开还是关闭。

这是另一个示例:http : //demo.guru99.com/test/radio.html

Python Selenium 选中 CheckBox 或者 Radio, Selenium 选中 单选框 或者 复选框, How to Select CheckBox and Radio Button in Selenium WebDriver
Python Selenium 选中 CheckBox 或者 Radio, Selenium 选中 单选框 或者 复选框, How to Select CheckBox and Radio Button in Selenium WebDriver

 

完整的代码

这是完整的工作代码

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) {									
    		
    	// declaration and instantiation of objects/variables		
        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"));							
        		
        //Radio Button1 is selected		
        radio1.click();			
        System.out.println("Radio Button Option 1 Selected");					
        		
        //Radio Button1 is de-selected and Radio Button2 is selected		
        radio2.click();			
        System.out.println("Radio Button Option 2 Selected");					
        		
        // Selecting CheckBox		
        WebElement option1 = driver.findElement(By.id("vfb-6-0"));							

        // This will Toggle the Check box 		
        option1.click();			

        // Check whether the Check box is toggled on 		
        if (option1.isSelected()) {					
            System.out.println("Checkbox is Toggled On");					

        } else {			
            System.out.println("Checkbox is Toggled Off");					
        }		
         
        		
        		
        //Selecting Checkbox and using isSelected Method		
        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(),则意味着在Web驱动程序访问页面时在页面中找不到该元素。

  1. 在Chrome中使用Firepath或Inspect Element再次检查定位器。
  2. 现在检查您在代码中使用的值是否与Firepath中的元素的值不同。
  3. 某些元素的某些属性是动态的。如果发现该值不同且正在动态变化,请考虑使用By.xpath()或By.cssSelector(),这两种方法更可靠但更复杂。
  4. 有时,这也可能是一个等待问题,例如,Web驱动程序甚至在页面完全加载之前就执行了代码,等等。
  5. 使用隐式或显式等待在findElement()之前添加一个等待。

 

摘要

  • 下表总结了访问上述每种元素的命令

 

Element Command Description
Check Box, Radio Button click() used to toggle the element on/off

 

本文: Python Selenium 选中 CheckBox 或者 Radio, Selenium 选中 单选框 或者 复选框, How to Select CheckBox and Radio Button in Selenium WebDriver

Loading

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.