java - 如何不使用 x 路径在 selenium webdriver 中选择单选按钮

标签 java selenium

带有我想要访问的单选按钮的页面片段:

<td style="font-size:xx-small"></td>
<td style="font-size:xx-small">
    <input type="radio" value="EMPLOYEE" name="user_type"></input>

    EMPLOYEE

    <input type="radio" checked="checked" value="CUSTOMER" name="user_type"></input>

    CUSTOMER

</td>

下面是测试用户与单选按钮交互的 Java 代码:

package com.ej.zob.modules;

import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.Select;

public class Login {
    public void Execute(String UserName,String Password,String Language,String Type)
    {
      LaunchApplication.driver.findElement(By.id("uname")).sendKeys(UserName);
      LaunchApplication.driver.findElement(By.id("pwd")).sendKeys(Password);

      Select sel = new Select(LaunchApplication.driver.findElement(By.name("language")));
      sel.selectByVisibleText(Language);
      //LaunchApplication.driver.findElement(By.name("language")).sendKeys(Language);
      String val=LaunchApplication.driver.findElements(By.name("user_type")).get(0).getText();
      //String value =LaunchApplication.driver.findElements(By.name("user_type")).get(1).getText();
      if(val.trim().equals("EMPLOYEE")){
        LaunchApplication.driver.findElement(By.name("EMPLOYEE")).click();          
      }
      LaunchApplication.driver.findElement(By.name("imageField")).click();      
    }
}

最佳答案

试试这个:

List<WebElement> list = driver.findElements(By.name("user_type"));
Iterator<WebElement> i = list.iterator();
while(i.hasNext()) 
{
WebElement rdButton = i.next();    
if(rdButton.getAttribute("value").contains("employee"))
{
   //select this radio button
   rdButton.click();
}
} 

关于java - 如何不使用 x 路径在 selenium webdriver 中选择单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29789270/

相关文章:

selenium - 通过命令行设置 Selenium RC 速度

java - 找不到参数的 dexOptions() 方法

java - Android应用程序中的可拖动 float 窗口: Not focusing on layout elements

java - 如何删除回车符和空格并将字符串正确存储在数组中?

java - 如何使用 Woodstox StAX 2 序列化/反序列化 XML 中的类

ruby - 等待 iframe 打开并加载 Selenium

java - Java 中的字符串

jquery - 无法单击图标的元素

java - Chromedriver 设置单一接受语言

python - 将 Selenium HTML 字符串传递给 Scrapy 以将 URL 添加到要抓取的 URL 的 Scrapy 列表