In this Chapter, we will see how to identify the following form elements
- Radio Button
- Check Box
Radio Button
Performing radio button select operation is very easy if you want to select the radio button without checking if any of the button is selected by default.these button and checkbox can be accessed by click() method. The difference between Radio button and Checkbox is that, using radio button we will be able to select only one option from the options available. whereas using checkbox, we can select multiple options.
Lets take an example of “mercurytours.com”.
For selecting Flight Details as Round Trip.
Here radio button present in flight details format.when we inspect above element we get html code as.
we can write following code:
WebElement radiobutton=driver.findElement(By.cssSeLector
("input[type='radio'][value='roundtrip']"));
radiobutton.click();
For selecting flight details as one way
we can write following code as:
WebElement radiobutton=driver.findElement(By.cssSeLector
("input[type='radio'][value='oneway']"));
radiobutton.click();
Complete Code:
package testology;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class radiobutton
{
public static void main(String[] args)
{
System.setProperty("webdriver.driver.chromedriver", "chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://newtours.demoaut.com/");
driver.findElement(By.linkText("SIGN-ON")).click();
WebElement name = driver.findElement(By.name("userName"));
name.sendKeys("Testology");
// get the webelement corresponding to password (passwordfield)
WebElement password = driver.findElement(By.name("password"));
password.sendKeys("Testology12");
// to click on submit button
WebElement submitbutton = driver.findElement(By.cssSelector
("input[name='login'][value='Login']"));
submitbutton.click();
//to select radiobuttons
WebElement radiobutton=driver.findElement(By.cssSelector
("input[type='radio'][value='roundtrip']"));
radiobutton.click();
WebElement radiobutton1=driver.findElement(By.cssSelector
("input[type='radio'][value='oneway']"));
radiobutton1.click();
}
}
Check Box
Switching of check box is done by using the click() method. For practise lets take an html example:
first checkbox try again