[英]how to select radio button in selenium webdriver by not using x path
The page fragment with the radio button I want to access: 我要访问的带有单选按钮的页面片段:
<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>
And below the Java code to test the user interaction with the radio button: 在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();
}
}
Try this: 尝试这个:
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();
}
}
a google of 'java selenium find element by' suggests: “ java selenium find element by”的谷歌建议:
etc.. 等等..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.