Selenium WebDriver- Locating Strategies and Handling Drop-downs

Last updated on Nov 29 2021
Manikaran Reddy

Table of Contents

Selenium WebDriver- Locating Strategies and Handling Drop-downs

Just like Selenium IDE, WebDriver uses an equivalent set of locating strategies for specifying location of a specific web element.
Since, we are using WebDriver with java; each locating strategy has its own command in Java to locate the online elements.
Note: Locating web elements in Webdriver is performed with the assistance of findElement() and findElements() method.
A list of Locating Strategies utilized in WebDriver:

Selenium Web Driver
Selenium Web Driver

• Locating Strategies By ID
• Locating Strategies By Name
• Locating Strategies By Class Name
• Locating Strategies By Tag Name
• Locating Strategies By Link Text
• Locating Strategies By Partial Link Text
• Locating Strategies By CSS
• Locating Strategies By XPath

 

Selenium WebDriver- Handling drop-downs
In this section, you’ll find out how to handle drop-downs in Selenium WebDriver.
Before proceeding with this section, allow us to first understand a number of the fundamentals of handling drop-downs in Selenium WebDriver.
Select in Selenium WebDriver
The ‘Select’ class in Selenium WebDriver is employed for choosing and deselecting option during a dropdown. The objects of Select type are often initialized by passing the dropdown webElement as parameter to its constructor.

1. WebElement testDropDown = driver.findElement(By.id("testingDropdown"));
2. Select dropdown = new Select(testDropDown);

How to select an option from drop-down menu?
WebDriver provides 3 ways to pick an option from the menu .
1. selectByIndex – it’s wont to select an option supported its index, beginning with 0.
1. dropdown.selectByIndex(5);
2. selectByValue – it’s wont to select an option supported its ‘value’ attribute.
1. dropdown.selectByValue(“Database”);
3. selectByVisibleText – it’s wont to select an option supported the text over the choice .
1. dropdown.selectByVisibleText(“Database Testing”);
Let us consider a test suit during which we’ll automate the subsequent scenarios:
• Invoke Google Chrome Browser
• Open URL: https://www.testandquiz.com/selenium/testing.html
• Select the choice “Database Testing” from the menu
• Close the browser
We will create our test suit step by step so as to offer you an entire understanding of the way to handle drop-downs in WebDriver.
Step1. Launch Eclipse IDE and open the prevailing test suite “Demo_Test” which we’ve created in earlier sessions of this tutorial.
Step2. Right click on the “src” folder and make a replacement Class File from New > Class.

Selenium Web Driver
Selenium Web Driver

Give your Class name as “Dropdwn_Test” and click on on “Finish” button.

Selenium Web Driver
Selenium Web Driver

Step3. Let’s get to the coding ground.
• To invoke Google Chrome browser, we’d like to download the ChromeDriver.exe file and set the system property “Running test on Chrome Browser” to the trail of your ChromeDriver.exe file. we’ve already discussed this in earlier sessions of this tutorial. you’ll also ask “Running test on Chrome Browser” to find out the way to download and set System property for Chrome driver.
Here is that the sample code to line system property for Chrome driver:

1. // System Property for Chrome Driver
2. System.setProperty("webdriver.chrome.driver","D:\\ChromeDriver\\chromedriver.exe");

• After that we’ve to initialize Chrome driver using ChromeDriver Class.
Here is that the sample code to initialize Chrome driver using ChromeDriver class.

1. // Instantiate a ChromeDriver class.
2. r driver=new ChromeDriver();

Combining both of the above code blocks, we’ll get the code snippet to launch Google Chrome browser.

1. // System Property for Chrome Driver
2. m.setProperty("webdriver.chrome.driver","D:\\ChromeDriver\\chromedriver.exe");
3. // Instantiate a ChromeDriver class.
4. river driver=new ChromeDriver();

• After that we’d like to write down the code which can automate our second test scenario (navigate to the specified URL).
Here is that the sample code to navigate to the specified URL:

1. // Launch Website
2. driver.navigate().to("https://www.testandquiz.com/selenium/testing.html");

The complete code till now will look something like this:

1. import org.openqa.selenium.WebDriver;
2. import org.openqa.selenium.chrome.ChromeDriver;
3.
4.
5.
6. public class Partial_Link {
7.
8. public static void main(String[] args) {
9.
10. // System Property for Chrome Driver
11. System.setProperty("webdriver.chrome.driver","D:\\ChromeDriver\\chromedriver.exe");
12.
13. // Instantiate a ChromeDriver class.
14. WebDriver driver=new ChromeDriver();
15.
16. // Launch Website
17. driver.navigate().to("https://www.testandquiz.com/selenium/testing.html");
18.
19.
20.
21. }
22.
23. }

Step4.Now we’ll attempt to locate the menu by inspecting its HTML codes.
Follow the steps given below to locate the menu on the sample website .
• Open URL: https://www.testandquiz.com/selenium/testing.html
• Right click on the menu on the sample website and choose Inspect Element

Selenium Web Driver
Selenium Web Driver

• It will launch a window containing all the precise codes involved within the development of the menu .

Selenium Web Driver
Selenium Web Driver

• Take a note of its id attribute.

Selenium Web Driver
Selenium Web Driver

Step5. To automate our third test scenario, we’d like to write down the code which can select the choice “Database Testing” from the menu .
Here is that the sample code to so that:

1. //Using Select class for choosing value from dropdown
2. Select dropdown = new Select(driver.findElement(By.id("testingDropdown")));
3. dropdown.selectByVisibleText("Database Testing");
4.
5. Thus, our final test script will look something like this:
6. import org.openqa.selenium.By;
7. import org.openqa.selenium.WebDriver;
8. import org.openqa.selenium.chrome.ChromeDriver;
9. import org.openqa.selenium.support.ui.Select;
10.
11. public class Dropdwn_Test {
12.
13. public static void main(String[] args) {
14.
15. // System Property for Chrome Driver
16. System.setProperty("webdriver.chrome.driver","D:\\ChromeDriver\\chromedriver.exe");
17.
18. // Instantiate a ChromeDriver class.
19. WebDriver driver=new ChromeDriver();
20.
21. // Launch Website
22. driver.navigate().to("https://www.testandquiz.com/selenium/testing.html");
23.
24.
25. //Using Select class for choosing value from dropdown
26.
27. Select dropdown = new Select(driver.findElement(By.id("testingDropdown")));
28. dropdown.selectByVisibleText("Database Testing");
29.
30. // Close the Browser
31. driver.close();
32.
33.
34. }
35. }

The following screenshot shows the Eclipse window for our test script.

Selenium Web Driver
Selenium Web Driver

Step6. Right click on the Eclipse code and choose Run As > Java Application.

Selenium Web Driver
Selenium Web Driver

Upon execution, the above test script will launch the Chrome browser and automate all the test scenarios.
So, this brings us to the end of blog. This Tecklearn ‘Selenium WebDriver – Locating Strategies and Handling Drop-downs’ blog helps you with commonly asked questions if you are looking out for a job in Selenium and Automation Testing. If you wish to learn Selenium and build a career in Automation Testing domain, then check out our interactive, Selenium Certification Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/selenium-training-certification/

Selenium Certification Training

About the Course

Tecklearn’s Selenium Certification Training enables you to master the complete Selenium suite. The Selenium Training is designed to train developers and manual testers to learn how to automate web applications with a robust framework, and integrate it within the DevOps processes of an organization. This Selenium Certification Training will also help you master important concepts such as TestNG, Selenium IDE, Selenium Grid, Selenium WebDriver, etc. Get hands-on experience on widely used automation frameworks such as Data-Driven Framework, Keyword-Driven Framework, Hybrid Framework, and Behavior Driven Development (BDD) Framework. Throughout this online Instructor-led Selenium Certification Training, you will be working on real-life industry use cases.

Why Should you take Selenium Certification Training?

• The average salary of a Selenium Test Automation Engineer is $94k per year – Indeed.com.
• Automation Testing Market is expected to grow at a Compound Annual Growth Rate (CAGR) of 18.0% in the next three years.
• Global software testing market to reach $50 billion by 2020 – NASSCOM. Selenium tool supports more browsers and languages than any other testing tool.

What you will Learn in this Course?

Getting started with Selenium
• Introduction to Selenium testing
• Significance of automation testing
• Comparison of Manual and Automation Testing
• Installation of Java JDK, JRE and Eclipse
Setting the environment in Eclipse for Selenium
• Java Introduction
• Creating a Java function and executing
• Concepts of Java
• Properties File
• Reading Data from Excel File
• Database Connection
• Hands On
Advantages of Selenium automation testing
• Selenium Features
• Concept of Selenium Integrated Development Environment
• Understanding of the Selenium IDE features
• Addition of Script assertions and general commands
• Deploying the first Selenium Script
• Sample project IDE
• Recording Selenium test case
• Hands On
Selenium Web driver Automation
• Architecture of Selenium Web Driver
• Download and installation
• Creating a Java function using Selenium and execution
• Hands On
Deploying Web Drivers for scripting
• Getting the HTML source of Web Element
• Table and Form Elements
• Firebug extension and Fire Path installation
• Advance User Interactions and Cross Browser Testing
• Hands On
Deep dive into Selenium Web Driver
• Action Commands
• Web Table / Date Picker
• How to Implement Switching Commands in WebDriver
• Alerts
• Frames
• Hands On
Switching Operations in WebDriver using Window
• Selenium Webdriver Wait
• Implicit wait, Explicit wait
• Deploying searching elements using the link text, name, using XPath
• Calendar
• Hands On
Introduction to TestNG Framework
• Introduction to TestNG
• Advantages of TestNG
• Installing TestNG on Eclipse
• Rules to write TestNG
• TestNG Features
• Annotations
• Grouping
• Sequencing: Prioritization and Dependency
• Enable/Disable a test case
• Parameterization: Using Xml file and DataProvider
• Parallel Testing & Cross Browser Testing
• TestNG Report: HTML Report, Console Report, XML Report
JUnit Operations and Test Framework
• Annotations, Methods in JUnit
• Junit Test Suites, ANT Build and JUNIT reporting
• Types of Test Automation Framework
• Module Based Testing Framework
• Data Driven Testing Framework
• Keyword Driven Testing Framework
• Hybrid Driven Testing Framework
• How to implement Testing Framework in Project
Object Repository
• Understanding of Object Repository
• Learning sample scripts using object repository
• Page Object Modelling
• Page Factory
JavaScript Functions
• Autosuggestion
• Headless Browser
• Sikuli
• XPath

Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "Selenium WebDriver- Locating Strategies and Handling Drop-downs"

Leave a Message

Your email address will not be published. Required fields are marked *