To select option from drop-down, you need to :
import the package : "org.openqa.selenium.support.ui.Select"
And create instance of “Select” class.
You can select the drop-down option by using following three methods:
selectByVisibleText("Option")
selectByIndex(Index);
selectByValue("Value");
1. selectByVisibleText("Option")
with this method, an option mentioned in the method is selected from the visible list of available options. In the example below, there are 13 options : Month , Jan, Feb, Mar, Apr, May , Jun , Jul, Aug, Sep , Oct, Nov, Dec.
Below code will open facebook.com page in chrome
browser and selects the month “Aug” from the drop-down list of months.
public static void main(String[] args) throws Exception {
System. setProperty("webdriver.chrome.driver", "chromeDriver_path\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
//maximize the chrome browser
driver.manage().window().maximize();
//enter the URL
driver.get("https://www.facebook.com/");
//Create instance of Select using the xpath of the month drop-down box.
Select selectMonth = new Select(driver.findElement(By.xpath("//select[@title='Month']")));
selectMonth.selectByVisibleText("Aug");
}
Before running the code |
After running the code |
2. selectByIndex(Index)
With this method, an option will be selected based on the given index number. The index starts from 0 and the last index number will be total number of options minus one as it starts from zero.
The code below will select “Feb” when we use the index 3 . Because as you can see , the index 0 = “Month”, index 1 = “Jan” , index 2 = “Feb” and so on.
public static void main(String[] args) throws Exception {
System. setProperty("webdriver.chrome.driver", "chromeDriver_path\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
//maximize the chrome browser
driver.manage().window().maximize();
//enter the URL
driver.get("https://www.facebook.com/");
Select selectMonth = new elect(driver.findElement(By.xpath("//select[@title='Month']")));
selectMonth.selectByIndex(2);
}
3. selectByValue("Value");
This will select the option based on the value provided(actually, value is attribute name and we have to select the respective attribute value ). If you see in the HTML, there is value as attributeName and the corresponding value as attributeValue assigned to each option. So, when you select an attributeValue as input , an option representing that value, will be selected .
Below example will select “May” when we select the attributeValue as “5” for the attributeName “value”.
public static void main(String[] args) throws Exception {
System. setProperty("webdriver.chrome.driver", "chromeDriver_path\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
//maximize the chrome browser
driver.manage().window().maximize();
//enter the URL
driver.get("https://www.facebook.com/");
Select selectMonth = new elect(driver.findElement(By.xpath("//select[@title='Month']")));
selectMonth.selectByValue("5");
Before running the code |
After running the code |
}
No comments:
Post a Comment