In my previous post I've explained how you can input a value to a text box. Now lets see how we deal with radio buttons.
when ever we give radio button as an options there is always more than one. So in java we have to treat these as "Lists". First we have to extract all the available options in radio button and do the task. So lets see how we can do that.
WebDriver A = new FirefoxDriver();
A.get("http://echoecho.com/htmlforms10.htm");
//Locating the radio button which has the values "Milk" and cliks on it
A.findElement(By.xpath("//input[@value='Milk']")).click();
//extracting all the radio button values to a list
List<WebElement> B = A.findElements(By.xpath(".//td[@class='table5']//input[(@type='radio')]"));
//Prints the size of the list
int count = B.size();
System.out.println(count);
//Prints the values in the list
for(WebElement z : B) {
String x = z.getAttribute("value");
System.out.println(x);
}
In above example i'm simply calling a URL which has a radio button options in it and extracting all the radio button values to a List.
Once we create a list we can use many type of functions to play with the data. Such as "add(), equals(), get(), isEmpty()" and so on. In this example to make sure that we have extracted the total number of radio button options i'm printing the size of the list.
in order to print the values inside the list (Actual radio button values) we can simply iterate the list inside the for loop.
for(WebElement z : B) {
String x = z.getAttribute("value");
System.out.println(x);
}
in above code i'm iterating the List elements to get the values one by one. Below code will assign the radio button value to variable "x" at each iteration. and "z.getAttribute("value"); " and getAttribute method will get the value of the given parameter. in this example i'm retrieving the text value inside the "Value" attribute.
String x = z.getAttribute("value");
Check radio button checked status
Sometimes we might have to check the status of the radio button, whether those are pre checked or not. Below code will be use full to check that status, You can just copy and paste this to your same class file
//Check if the values are checked at the initial open
for (int i = 0; i <B.size(); i++) {
if (B.get(i).getAttribute("Checked") != null)
System.out.println(B.get(i).getAttribute("value")+"Is Cheked");
else
System.out.println(B.get(i).getAttribute("value")+"Is not checked");
}
If you want specifically click on specific radio button you can use below code.
String Xpath3 = "//table[3]/tbody/tr/td/table/tbody/tr/td/input[1]";
WebElement elementToClick = A.findElement(By.xpath(Xpath3));
elementToClick.click();
or may be inside a loop
for (int i = 1; i <B.size(); i++) {
if (B.get(i).getAttribute("Checked") != null){
WebElement elementToClick = A.findElement(By.xpath(Xpath1+i+Xpath2));
elementToClick.click();
} }
you Selenium experts might have better, easier ways of doing this, and i would love your command and to know other possible options. and please do comment if there is any code that i can improve of.
No comments:
Post a Comment