Selenium教程

Selenium 处理单选按钮

在本节中,您将学习如何在Selenium Web驱动程序中处理单选按钮。
以下是步骤: 处理单选按钮:
步骤1: 调用Google Chrome浏览器。
下面给出了调用Google chrome浏览器的代码。 :
package mypack;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Class1 
{
    public static void main(String[] args) 
    {
        
        System.setProperty("webdriver.chrome.driver", "C:\\work\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        
    }
}
第2步: 第二步是导航到需要处理单选按钮的网站。
我创建了html文件,其中包含单选按钮。代码如下:
<html>
<head>
</head>
<body>
<input type="radio" name="group1" value="Mango">Mango<br>
<input type="radio" name="group1" value="Watermelon">Mango<br>
<input type="radio" name="group1" value="Banana">Mango<br>
<hr>
<input type="radio" name="group2" value="Ladyfinger">Ladyfinger<br>
<input type="radio" name="group2" value="Potato">Potato<br>
<input type="radio" name="group2" value="Tomato">Tomato<br>
</body>
</html>
导航到上述html文件的代码如下:
package mypack;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Class1 {
    public static void main(String[] args) {
        
        System.setProperty("webdriver.chrome.driver", "C:\\work\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("file:///C:/Users/admin/Desktop/radio.html");
        }
}
上述代码的输出:
处理单选按钮
步骤3 : 选择"香蕉"选项。我们将通过检查其HTML代码来找到Banana单选按钮。
有两种处理单选按钮的方法:
使用自定义路径:
下面显示的代码通过使用自定义路径来处理单选按钮。
package mypack;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Class1 {
    public static void main(String[] args) {
        
        System.setProperty("webdriver.chrome.driver", "C:\\work\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("file:///C:/Users/admin/Desktop/radio.html");
        driver.findElement(By.xpath("//input[@value='Banana']")).click();
        }
}
在上面,我们使用自定义Xpath。单选按钮包含唯一属性,即value,因此我们使用value属性处理单选按钮。
输出
处理单选按钮 通过动态处理单选按钮。 我们将首先计算单选按钮的数量。以下是计算单选按钮数量的代码行。

int a = driver.findElements(By.xpath("//input [@ name ='group1']"))) .size();

上面的代码行计算名称为group1的单选按钮的数量。
现在,我们将使用特定单选按钮的索引来处理单选按钮。

driver.findElements(By.xpath("//input [@ name ='group1'] "))。get(2).click();
源代码
package mypack;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Class1 {
    public static void main(String[] args) {
        
        System.setProperty("webdriver.chrome.driver", "C:\\work\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("file:///C:/Users/admin/Desktop/radio.html");
        int a = driver.findElements(By.xpath("//input [@name='group1']")).size();
        System.out.println(a);
        for(int i=1;i<=a;i++)
        {
            driver.findElements(By.xpath("//input[@name='group1']")).get(2).click();
        }
    }}
在上面的代码中,我们使用" for"循环。在" for"循环中,我们使用get(2)方法找到了group1的第三个单选按钮。
输出
处理广播按钮
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4