开发者

How to select radio button when two or three radio buttons are there, using selenium

开发者 https://www.devze.com 2023-03-01 13:07 出处:网络
I am looking for selenium code using java on how to select a particular radio button when multiple radio buttons are there in a form.

I am looking for selenium code using java on how to select a particular radio button when multiple radio buttons are there in a form.

For one radio button it is开发者_如何学编程 Ok with selenium.click("radio1"), but when in the above case

I.E., I am reading from excel sheet

Please help me in this regard


You can have multiple radio buttons with the same name. Therefore you will need to select either by an id attribute (which must be unique per element), or based on the value attribute (which I can only presume is different)... or by positional index (but this is a somewhat fragile approach)

e.g. use something like this

selenium.click("id=idOfItem");
selenium.click("xpath=//input[@value='Blue']");//select radio with value 'Blue'


Use selenium.check("name=<name> value=<value>");.

Note that <name> is the same for all of the buttons, but <value> will be different.


// 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;
    }
   }
0

精彩评论

暂无评论...
验证码 换一张
取 消