【发布时间】:2011-08-14 17:47:57
【问题描述】:
我正在寻找使用 java 的 selenium 代码,了解如何在表单中有多个单选按钮时选择特定单选按钮。
对于一个单选按钮,selenium.click("radio1") 可以,但在上述情况下
I.E.,我正在阅读 excel 表格
请在这方面帮助我
【问题讨论】:
标签: java selenium radio-button
我正在寻找使用 java 的 selenium 代码,了解如何在表单中有多个单选按钮时选择特定单选按钮。
对于一个单选按钮,selenium.click("radio1") 可以,但在上述情况下
I.E.,我正在阅读 excel 表格
请在这方面帮助我
【问题讨论】:
标签: java selenium radio-button
您可以有多个同名的单选按钮。因此,您将需要通过 id 属性(每个元素必须是唯一的)或基于 value 属性(我只能假设它是不同的)进行选择...或通过位置索引(但这是一种有点脆弱的方法)
例如使用类似的东西
selenium.click("id=idOfItem");
selenium.click("xpath=//input[@value='Blue']");//select radio with value 'Blue'
【讨论】:
使用selenium.check("name=<name> value=<value>");。
请注意,<name> 对于所有按钮都是相同的,但 <value> 会有所不同。
【讨论】:
// get all the radio buttons by similar id or xpath and store in List
List<WebElement> radioBx= driver.findElements(By.id("radioid"));
// This will tell you the number of radio button are present
int iSize = radioBx.size();
//iterate each link and click on it
for (int i = 0; i < iSize ; i++){
// Store the Check Box name to the string variable, using 'Value' attribute
String sValue = radioBx.get(i).getAttribute("value");
// Select the Check Box it the value of the Check Box is same what you are looking for
if (sValue.equalsIgnoreCase("Checkbox expected Text")){
radioBx.get(i).click();
// This will take the execution out of for loop
break;
}
}
【讨论】: