gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:38:12 26 4
gpt4 key购买 nike

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

<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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com