How To Handle The drop Down in selenium Web Driver?
In Selenium 'Select' class is used
for selecting and deselecting option in a dropdown.
In This Chapter We are Going to
Learn how to handle Drop Down
we can control drop-down boxes We
have to do 2 things-
Step-1 Import the package
org.openqa.selenium.support.ui.Select
Step-2 Instantiate the drop-down box as a
"Select" object in WebDriver
DropDown Method For selecting options
–
selectByValue();-it will select the
option by value
selectByIndex();-it will select the
option by index number
selectByVisibleText();-it will select
the option by visible text
DropDown Method For Deselecting options
–
deselectAll() - it will deselect all the Selected option
deselectByVisibleText()- it will
deselect the option by visible text
deselectByValue() - it will deselect
the option by value.
deselectByIndex()-it will deselect
the option by index number
Syantax-
Select drpdn = new Select(driver.findElement(By.xpath("//*[@id=\"blog-cat-dropdown\"]")));
drpdn1.selectByValue("/blog/social-media/");
Selenium Code
for handling Drop down-
package automationtesting;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import
org.openqa.selenium.chrome.ChromeDriver;
import
org.openqa.selenium.support.ui.Select;
public class HandlingDropDn {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","D:\\Sandeep\\Soft\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.webfx.com/blog/web-design/50-examples-of-drop-down-navigation-menus-in-web-designs/");
Select drpdn = new Select(driver.findElement(By.xpath("//*[@id=\"blog-cat-dropdown\"]")));
drpdn.selectByVisibleText("INTERNET");
Thread.sleep(2000);
Select drpdn1 = new Select(driver.findElement(By.xpath("//*[@id=\"blog-cat-dropdown\"]")));
drpdn1.selectByValue("/blog/social-media/");
driver.quit();
}
}
0 Comments