How to select Radio Buttons, Drop downs using Selenium webdriver

In this post,I will be going to discuss about selecting Radio Buttons and Drop downs using Selenium webdriver.

Drop downs are selected in many ways. Here I will be discussing how an element from drop down is getting selected using SELECT TAG. The select Tag, you can find in the HTML. There are ‘selectByVisibleText’ , ‘selectByIndex’ and ‘selectByValue’ methods.

Lets take an example of Yahoo registration page.

  1. Login in to yahoo site ( ” https://in.yahoo.com “)
  2. By using mouse hover action in Sign-in field, click on sign up.
    More details in mouse hover action you can find on https://siprabugtracker.wordpress.com/2014/11/25/mouse-hover-action-using-selenium-webdriver/
  3. From the Date field, select a date (6th) from the Drop Down.
  4. From the Month field, select a Month (January) from the Drop Down.
  5. From the Year field, select a year (2013) from the Drop Down.
  6. From the Gender section, click on the female radio button.
  7.  Don’t close the browser as you will see the output as 6 January 2013, Female.
package package1;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

public class Yahoo_registration {

	public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
// Initialize WebDriver
WebDriver driver = new FirefoxDriver();

// Maximize Window
  driver.manage().window().maximize();
  
// Wait For Page To Load
 driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

 //Navigate to Yahoo webstites
 driver.get("https://in.yahoo.com/?p=us");

// Mouse Over On " Sign-In button  " 
 
 Actions a1 = new Actions(driver);
 a1.moveToElement(driver.findElement(By.xpath("//em[@title='Sign In']"))).build().perform();
 Thread.sleep(3000L);
 System.out.println("clicked on the Sig-in button");
 
// Wait For Page To Load
 driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
 
 //Click on the Sign Up Button
 driver.findElement(By.xpath("//span[@class='y-hdr-ln sign-up-btn']/a")).click();
 System.out.println("clicked on the Sig-Up button");
   
     
// so if a web drop down is getting developed using SELECT TAG in html the following code is implemented 
// -----------
// Select Date 
Select date =new Select(driver.findElement(By.id("day")));
 	
// Select date ["2"]  by Index  
// date.selectByIndex(2);
 
 
// Select date ["6"]  by Value        
date.selectByValue("6");	
driver.findElement(By.id("day")).getText();

 System.out.println("Date = 6th");
   
// Select MONTH 
 Select month =new Select(driver.findElement(By.id("month")));

 // Select month ["MAY"]  by Index  
 month.selectByIndex(5);
 
 // Select month ["OCTOBER"]  by Value        
 month.selectByValue("10");
   
 // Select month ["January"]  by Visible Text	     
 month.selectByVisibleText("January");
     
 System.out.println("month = January");
     
 // --------------
     
// Select Year

Select year =new Select(driver.findElement(By.id("year")));
    
// Select year ["2012"]  by Index
  
year.selectByIndex(3);
System.out.println("Year = 2013");
 

// -------------
// Select Gender
 driver.findElement(By.xpath("//*[@id='gender-wrapper']/fieldset/div/label[2]")).click();

 
 System.out.println("Gender = Female");
   
 System.out.println("The Output of the Gender(Female) Is Enabled : " + driver.findElement(By.xpath("//*[@id='gender-wrapper']/fieldset/div/label[2]")).isEnabled());
 System.out.println("The Output of the Gender Is Displayed : " + driver.findElement(By.xpath("//*[@id='gender-wrapper']/fieldset/div/label[2]")).isDisplayed());
     
}
	    

}


Hope this helps the beginners.

Thanks for reading.

 

2 thoughts on “How to select Radio Buttons, Drop downs using Selenium webdriver

  1. kalyan singh says:

    Hai,sipra

    In some some application i can able to select radio button simply by using id

    But in another application i had to use click(),isEnabled() & isDisplayed() why this difference pls let me know give reply to my mail if possible i.e “kalyan.singh131@gmail.com”

    Like

Leave a comment