许多网站使用社交媒体登录来简化用户的登录过程。在大多数情况下,如果单击该按钮,则会打开一个新的弹出窗口,用户必须在其中输入用户凭据。可以手动在浏览器中切换窗口并输入所需的凭据以登录。但是,如果使用webdriver进行无人值守的Web访问,则驱动程序不能仅自动切换窗口。我们需要更改驱动程序中的窗口句柄,以便在弹出窗口中输入登录凭据。Selenium具有使用同一驱动程序切换窗口以访问多个窗口的功能。 首先,我们必须从Webdriver获取当前的窗口句柄,这可以通过以下方式完成: driver.current_window_handle 我们需要保存它以获取当前的窗口句柄。弹出窗口出现后,我们必须立即获取所有可用窗口句柄的列表。 driver.window_handles …
January 24, 2020
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](https://justcode.ikeepstudying.com/wp-content/uploads/2020/01/072717_0632_SeleniumWeb8.png)
复选框
也可以使用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](https://justcode.ikeepstudying.com/wp-content/uploads/2020/01/072717_0632_SeleniumWeb9.png)
![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](https://justcode.ikeepstudying.com/wp-content/uploads/2020/01/072717_0632_SeleniumWeb10.png)
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](https://justcode.ikeepstudying.com/wp-content/uploads/2020/01/072717_0632_SeleniumWeb11.png)
完整的代码
这是完整的工作代码
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驱动程序访问页面时在页面中找不到该元素。
- 在Chrome中使用Firepath或Inspect Element再次检查定位器。
- 现在检查您在代码中使用的值是否与Firepath中的元素的值不同。
- 某些元素的某些属性是动态的。如果发现该值不同且正在动态变化,请考虑使用By.xpath()或By.cssSelector(),这两种方法更可靠但更复杂。
- 有时,这也可能是一个等待问题,例如,Web驱动程序甚至在页面完全加载之前就执行了代码,等等。
- 使用隐式或显式等待在findElement()之前添加一个等待。
摘要
- 下表总结了访问上述每种元素的命令
Element | Command | Description |
---|---|---|
Check Box, Radio Button | click() | used to toggle the element on/off |