Selenium WebDriver – Navigation and Web Element Commands

Last updated on Dec 02 2021
Manikaran Reddy

Table of Contents

Selenium Web Driver – Navigation and Web Element Commands

Selenium WebDriver – Navigation and Web Element Commands
WebDriver provides some basic Browser Navigation Commands that allows the browser to maneuver backwards or forwards within the browser’s history.
Just like the browser methods provided by WebDriver, we’ll also access the navigation methods provided by WebDriver by typing driver.navigate() within the Eclipse panel.

Selenium Web Driver
Selenium Web Driver

Note: The methods having ‘Navigation’ as keyword are declared as Navigation commands.
Given are variety of the foremost commonly used Browser Navigation commands for Selenium WebDriver.
1. Navigate To Command
Method:

1. to(String arg0) : void

In WebDriver, this method loads a replacement website within the prevailing browser window. It accepts String as parameter and returns void.
The respective command to load/navigate a replacement website are often written as:

1. driver.navigate().to("www.tecklearn.com");

Note: The get command (driver.get(URL);) which lies within the browser commands section does the same function because the navigate command

1. (driver.navigate().to("www.tecklearn.com");

2. Forward Command
Method:

1. to(String arg0) : void

In WebDriver, this method enables the web browser to click on the forward button within the prevailing browser window. It neither accepts anything nor returns anything.
The respective command that takes you forward by one page on the browser’s history are often written as:

1. driver.navigate().forward();
3. Back Command
Method:

1. back() : void

In WebDriver, this method enables the web browser to click on the rear button within the prevailing browser window. It neither accepts anything nor returns anything.
The respective command that takes you back by one page on the browser’s history are often written as:

1. driver.navigate().back();

4. Refresh Command
Method:

1. refresh() : void

In WebDriver, this method refresh/reloads this website within the prevailing browser window. It neither accepts anything nor returns anything.
The respective command that takes you back by one page on the browser’s history are often written as:

1. driver.navigate().refresh();

Let us consider a sample test script which may cover most of the Navigation Commands provided by WebDriver.
In this sample test, we’ll automate the next test scenarios:
• Invoke Firefox Browser
• Navigate to URL: https://www.testandquiz.com/selenium/testing.html
• Click on the “This could also be a link” link (This link will redirect you to the tecklearn website)
• come to the house page using the rear command
• Again return to the tecklearn website using forward command
• Again come to the house page using To command
• Refresh the Browser using Refresh command
• Close the Browser
For our test purpose, we are employing a dummy website under the URL:
https://www.testandquiz.com/selenium/testing.html (You can also use this dummy website for your Selenium Test Practices)
• Step1. Launch Eclipse IDE and open the prevailing test suite “Demo_Test” which we’ve created in WebDriver Installation section of WebDriver 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 “Navigation_command” and click on on on “Finish” button.

Selenium Web Driver
Selenium Web Driver

Step3. Let’s get to the coding ground.
• To invoke Firefox browser, we’d wish to download Gecko driver and set the system property for Gecko driver.
Here is that the sample code to line system property for Gecko driver:

1. // System Property for Gecko Driver System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" )

After that we’ve to initialize Gecko Driver using Desired Capabilities Class.
Here is that the sample code to initialize gecko driver using DesiredCapabilities class.

1. // Initialize Gecko Driver using Desired Capabilities Class
2. DesiredCapabilities capabilities = DesiredCapabilities.firefox();
3. capabilities.setCapability("marionette",true);
4. WebDriver driver= new FirefoxDriver(capabilities);

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

1. // System Property for Gecko Driver
2. System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );
3.
4. // Initialize Gecko Driver using Desired Capabilities Class
5. DesiredCapabilities capabilities = DesiredCapabilities.firefox();
6. capabilities.setCapability("marionette",true);
7. WebDriver driver= new FirefoxDriver(capabilities);

• then we’d wish to write down the code which may automate our second test scenario (get the required URL)
Here is that the sample code to navigate to the required URL:

1. //Navigate to the required URL

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

• To automate our third test scenario, first we’ve to uniquely identify the “This could also be a link” link on the dummy test page.
The method for locating a singular identification element involves inspection of HTML codes.
1. Open URL: https://www.testandquiz.com/selenium/testing.html in your firefox browser.
2. Right click on the “This could also be a link” link text and choose Inspect Element.

Selenium Web Driver
Selenium Web Driver

It will launch a window containing all the precise codes involved within the event of the “This could also be a link” link. Select the name of the link text from inspector text box.

Selenium Web Driver
Selenium Web Driver

The Java Syntax for uniquely identifying an online element through its Link Text is written as:

1. driver.findElement(By.linkText (<linktext>)

Therefore, for locating the Link Text on the sample website we’ll use the price of its Link Text:

1. driver.findElement(By.linkText ())

Now, we’d wish to write down the code which may click on the Link Text.
Here is that the sample code to click on the Link Text.

1. // Click on the Link Text using click() command driver.findElement(By.linkText("This could also be a Link")).click();

On click, the link will redirect the browser window to the official website of tecklearn website.

• To automate our fourth test scenario, we’ve to revert the action performed by our third test scenario. to undertake to thereto , we’ll use the rear command to undo the action performed on click of the link text.
Here is that the sample code to return to the house page after being directed to the tecklearn website.

1. // return to Home Page

2. driver.navigate().back();

• Now, subsequent test scenario requires us to again attend the action performed by our third test scenario i.e., the window will again directed to the tecklearn website.
Here is that the sample code to travel forward again to the official website of tecklearn website.

1. // proceed to Registration page

2. driver.navigate().forward();

• Now, to automate our sixth test scenario, we’ll require to again navigate to the house page of dummy website by using the To command.
Here is that the sample code to travel back to the house page.

1. // return to Home page

2. driver.navigate().to(appUrl);

• To refresh the browser window, use the Refresh command as:

1. // Refresh browser

2. driver.navigate().refresh();

• Finally, the given code snippet will terminate the tactic and shut the browser.

1. driver.close();

Combining all of the above code blocks together, we’ll get the required ASCII document to execute our test script “Navigation_command”.
The final test script will appear something like this:
(We have embedded comment in each section to elucidate the steps clearly)

1. import org.openqa.selenium.By;
2. import org.openqa.selenium.WebDriver;
3. import org.openqa.selenium.firefox.FirefoxDriver;
4. import org.openqa.selenium.remote.DesiredCapabilities;
5.
6. public class Navigation_command {
7.
8. public static void main(String[] args) {
9.
10. // System Property for Gecko Driver
11. System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );
12.
13. // Initialize Gecko Driver using Desired Capabilities Class
14. DesiredCapabilities capabilities = DesiredCapabilities.firefox();
15. capabilities.setCapability("marionette",true);
16. WebDriver driver= new FirefoxDriver(capabilities);
17.
18. // Launch Website
19. driver.navigate().to("https://www.testandquiz.com/selenium/testing.html");
20.
21. //Click on the Link Text using click() command
22. driver.findElement(By.linkText("This could also be a Link")).click();
23.
24. //Go back to Home Page
25. driver.navigate().back();
26.
27. //Go forward to Registration page
28. driver.navigate().forward();
29.
30. // return to Home page
31. driver.navigate().to("https://www.testandquiz.com/selenium/testing.html");
32.
33. //Refresh browser
34. driver.navigate().refresh();
35.
36. //Closing browser
37. driver.close();
38. }
39. }

To run the test script on Eclipse window, right click on the screen and click on on
Run as → Java application

Selenium Web Driver
Selenium Web Driver

Upon execution the test script will launch the Firefox browser and automate all the test scenarios.
Selenium WebDriver – WebElement Commands
Before proceeding with this section, first we should always always know the essential terminology related to web elements in WebDriver.
What is Web Element?
The term web element refers to a HTML element. The HTML documents are composed of HTML elements. It consists a start tag, an end tag and thus the content in between. as an example , a HTML element is written as: ” content “
In WebDriver, we’ve several commonly used web element commands and actions. the next screenshot displays the eclipse web element command panel.

Selenium Web Driver
Selenium Web Driver

Note: to urge the web element object, we’ve to write down down the statement as:

1. WebElement element = driver.findElement(By.id("UserName"));

Here, the UserName is that the worth of the id attribute, used as a singular identification for the required web element.
Given are variety of the foremost commonly used WebElement commands for Selenium WebDriver.
1. Clear Command
Method:

1. clear() : void

Command:

1. element.clear();

Code snippet:

1. WebElement element = driver.findElement(By.id("UserName"));
2. element.clear();
3.
4. //Or are often written as
5.
6. driver.findElement(By.id("UserName")).clear();

2. Sendkeys Command
Method:

1. sendKeys(CharSequence? KeysToSend) : void

Command:

1. element.sendKeys("text");

Code snippet:

1. WebElement element = driver.findElement(By.id("UserName"));
2. element.sendKeys("Tecklearn");
3.
4. //Or are often written as
5.
6. driver.findElement(By.id("UserName")).sendKeys("Tecklearn");

3. Click Command
Method:

1. click() : void

Command:

1. element.click();

Code snippet:

1. WebElement element = driver.findElement(By.linkText("tecklearn"));
2. element.click();
3.
4. //Or are often written as
5.
6. driver.findElement(By.linkText("tecklearn")).click();

4. IsDisplayed Command
Method:

1. isDisplayed() : boolean

Command:

1. element.isDisplayed();

Code snippet:

1. WebElement element = driver.findElement(By.id("UserName"));
2. boolean status = element.isDisplayed();
3.
4. //Or are often written as
5.
6. boolean staus = driver.findElement(By.id("UserName")).isDisplayed();

5. IsEnabled Command
Method:

1. isEnabled() : boolean

Command:

1. element.isEnabled();

Code snippet:

1. WebElement element = driver.findElement(By.id("UserName"));
2. boolean status = element.isEnabled();
3.
4. //Or are often written as
5.
6. boolean staus = driver.findElement(By.id("UserName")).isEnabled();
7.
8. //Or are often used as
9. WebElement element = driver.findElement(By.id("userName"));
10. boolean status = element.isEnabled();
11. // confirm if the Text field is enabled, if yes enter value
12. if(status){
13. element.sendKeys("tecklearn");
14. }
6. IsSelected Command

Method:

1. isSelected() : boolean

Command:

1. element.isSelected();

Code snippet:

1. WebElement element = driver.findElement(By.id("Sex-Male"));
2. boolean status = element.isSelected();
3.
4. //Or are often written as
5.
6. boolean staus = driver.findElement(By.id("Sex-Male")).isSelected();
7. Submit Command

Method:

1. submit() : void
Command:
1. element.submit();

Code snippet:

1. WebElement element = driver.findElement(By.id("SubmitButton"));
2. element.submit();
3.
4. //Or are often written as
5.
6. driver.findElement(By.id("SubmitButton")).submit();
8. GetText Command

Method:

1. getText() : String

Command:

1. element.getText();

Code snippet:

1. WebElement element = driver.findElement(By.xpath("anyLink"));
2. String linkText = element.getText();
9. GetTagName Command

Method:

1. getTagName() : String

Command:

1. element.getTagName();

Code snippet:

1. WebElement element = driver.findElement(By.id("SubmitButton"));
2. String tagName = element.getTagName();
3.
4. //Or are often written as
5.
6. String tagName = driver.findElement(By.id("SubmitButton")).getTagName();
10. getCssValue Command

Method:

1. getCssvalue() : String

Command:

1. element.getCssValue();
11. getAttribute Command

Method:

1. getAttribute(String Name) : String

Command:

1. element.getAttribute();

Code snippet:

1. WebElement element = driver.findElement(By.id("SubmitButton"));
2. String attValue = element.getAttribute("id"); //This will return "SubmitButton"
12. getSize Command

Method:

1. getSize() : Dimension

Command:

1. element.getSize();

Code snippet:

1. WebElement element = driver.findElement(By.id("SubmitButton"));
2. Dimension dimensions = element.getSize();
3. System.out.println("Height :" + dimensions.height + "Width : "+ dimensions.width);
13. getLocation Command

Method:

1. getLocation() : Point

Command:

1. element.getLocation();

Code snippet:

1. WebElement element = driver.findElement(By.id("SubmitButton"));
2. Point point = element.getLocation();
3. System.out.println("X cordinate : " + point.x + "Y cordinate: " + point.y);

So, this brings us to the end of blog. This Tecklearn ‘Selenium WebDriver – Navigation and Web Element Commands’ 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 - Navigation and Web Element Commands"

Leave a Message

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