Good Friday Sale : Flat 30% off on live classes + 2 free self-paced courses! - SCHEDULE CALL

- Selenium Blogs -

Different Selenium Web Driver Commands

Introduction

Hopefully, you have completed the setup for Selenium. You have configured Selenium Jar and Browser in your system. Now you are good to learn with Selenium Webdriver Commands. I will cover all important Selenium Commands which you can use in your Test Scripts.

Selenium WebDriver Commands

  • Do you know how to access the Webdriver command?
  • What are these selenium commands?
  • Why are these so important?
  • Can you write your test script without using Webdriver commands?

So many questions might be floating in your mind.

Let me give you answers to your questions. Webdriver commands can be accessed by creating a driver object. You just need to create an instance of Webdriver and then all the commands of Driver you can access. Without using these commands you can't even access your web URL.

You can see in the image below the total number of Selenium Webdriver commands which are divided into 3 parts.

Selenium Webdriver commands

Selenium Webdriver command list

Selenium Webdriver command list

I will explain each Selenium command in detail one by one.

Browsers Command

These are those commands which directly interact with your browser. For accessing the commands just create an object of WebDriver. With the help of this object, you can use all Browser commands. You can see in the below image all get commands.

 Browsers Command

Get Command: How to open a Webpage

This is the very first command used in every test script. Before performing any action on a webpage it is required to open that webpage. So, Get command is used to access and open the WebPage in the current browser window by taking the URL name as a parameter. It returns nothing as void is the return type.

Command: get(string arg0):void - WebDriver

Syntex: driver.get(WebSiteURL);

Example:

driver.get(“https://www.facebook.com”);
OR
String URL=”https://www.facebook.com”
driver.get(URL);

GetTitle Command : How to get the title of Webpage

This command is used to fetch the title of the current webpage opened. It does not take any argument as a parameter but returns a String value.

Command: getTitle() : String - WebDriver

Syntex:  

String title=driver.getTitle();
OR
driver.getTitle();

getTitle() : String - WebDriver

Example:

title command

GetPageSource Command : How to get the page source of WebPage

This command is used to fetch the page source of the webpage. It does not take any argument as a parameter but returns a String value. The same source code you get as when you right click on a page and choose View Page source to check the HTML code of your webpage.

Command: getpageSource() : WebDriver

Syntex:

driver.getPageSource();
OR
String driver.getPageSource();

getCurrentURL: How to get the current page URL

This command is used to get the URL of the current page. When you work on multiple web pages then this command is used to navigate on multiple pages. It does not take any argument as a parameter but returns a String value.

Command: getCurrentUrl : String - WebDriver

Read: Selenium Tutorials for Beginners: A Complete Guide to Master Selenium

Syntex:

String URL=driver.getCurrentUrl();
OR
driver.getCurrentUrl();

Example:

Close : How to close the current Webpage or you can say browser

The close command is used to close the current browser window which is webdriver controlling. It neither takes any argument as a parameter nor returns any value.

Command: close() : void - WebDriver

Syntex : driver.close();

Example:

Quit : How to close all open session

This command is used to close all open windows of the browser. If you are working on Multiple windows then this command is used to close all open windows in one go. It neither takes any argument as a parameter nor returns any value.

Command: quit() : void - WebDriver

Syntex : driver.quit();

Example:

There is one question that might come in your mind. What is the difference between quit and close command? This is a famous interview question which is mostly asked in interviews.

Let me give you an answer to this. Quit command closes all the open windows but close command only closes a single-window on which the driver has control. 

Actually when your script works on multiple windows, so, in that case, quit command is useful as you want to close all opened windows. But in case your script works on a single browser window then you can use close command.

Don’t worry I will explain these with an example so that you could understand clearly.

Selenium Webdriver Browser commands using close: Practice Example 1 

Test case1:

Test Step:

  1. Launch the chrome browser.
  2. Access “ www.facebook.com”
  3. Get the title of the page and verify it with Given Title
  4. Get the URL and verify with the Given URL
  5. Get the pageSouce and find the length of it
  6. Print Title,URL and Page Source length on Eclipse console
  7. Close the session.

Test Script 1: Here you can see that the verification part is done without applying assertion. For simplicity, I have verified using the Simple String function. I will cover Assertion in upcoming blogs.

Test Steps in Script :

1. Test Steps in Script :

Launch the chrome browser.

WebDriver driver=new ChromeDriver();

2. Access “ www.facebook.com”

driver.get("https://www.facebook.com");

3. Get the title of the page and verify it with Given Title

String ExpectedTittle="Facebook – log in or sign up";
String Actualtitle=driver.getTitle();
Boolean result1=Actualtitle.equalsIgnoreCase(ExpectedTittle);

Read: What is Xpath in Selenium? How to Write Xpath Selenium?

4. Get the URL and verify with the Given URL

String ExpectedURL="https://www.facebook.com/";    
String ActualURL=driver.getCurrentUrl();
Boolean result2=ActualURL.equalsIgnoreCase(ExpectedURL);

5. Get the pageSouce and find the length of it

length=driver.getPageSource().length();

6. Print Title,URL and Page Source length on Eclipse console

System.out.println("Title of Webpage is "+Actualtitle);
System.out.println("URL of webpage is "+ActualURL);
System.out.println("Length of Page Source is "+length);

7. Close the session.

driver.close();

Selenium Webdriver Browser commands using quit command: Practice Example 2

Test Case 2:

Test Steps:

  1. Launch the chrome browser.
  2. Access “www.facebook.com”
  3. Click on Terms link. A new page will open.
  4. Click on Data Policy. A new page will open
  5. Close all the open windows.

Test Script 2:

Here you can see that two new pages are opening. So close all webpage quit commands you can use. To act on a newly opened page you have to switch on that page. You will learn this in my next blogs.

Navigation Command

Now you have run successfully your first scripts with all-important Browser commands. Now we are moving to Navigation Browser commands.

In a similar way to Browser commands, Navigation commands used to access a webpage.  In case you want to refresh and move forward and back the webpage then these commands are used. You just need to create an object of Webdriver. With the help of that object, you can use all Navigation commands. You can see in the image below all Navigation Selenium web driver Command.

Navigation Command

Navigate.to command :

In web driver, This method is used to load a new webpage in an existing window. It does take an argument as a parameter in the form of URL which you want to access and return nothing.

Command 

Syntex: driver.navigate().to(“String URL”);

Example :  

driver.navigate().to("https://www.facebook.com");
OR 
String URL=”https://www.facebook.com”;
driver.navigate().to(URL);

Refresh the Webpage command

Sometimes it happens that you need to refresh the page to load the data properly or Data is getting updated when you refresh the page so in that case, this command is useful. It neither takes an argument as a parameter nor returns anything.

Command: refresh() : void - Navigation

Syntex : driver.navigate().refresh();

Back Command 

The navigate command maintains the history of the page so in case you want to go back to the webpage then you can use this command. This command enables the web browser to click on the back button. It neither takes an argument as a parameter nor returns anything.

Command : back() : void - Navigation

Read: Selenium Testers Role: Job Responsibilities & Description

Syntex : driver.navigate().back();

Forward Command

The navigate command maintains the history of the page so in case you want to go to the next webpage then you can use this command. This command enables the web browser to click on the forward button. It neither takes an argument as a parameter nor returns anything.

Command : forward() : void - Navigation

Syntex : driver.navigate().forward().

All Navigation commands are shown in the below image.

Here one question could arise in you mind that Get and Navigate both commands are used to access the webpage URL then what is the difference between them

Let me clear your doubt and this is another most important interview question.

driver.get() driver.navigate().to
It is used to access a particular website but this method doesn’t maintain the browser history so you can not use the Back and Forward method It is used to access a particular website but this method maintains the browser history so you can use back and forward method
This method waits until a webpage fully loaded before returning the control to your test script. This method doesn't wait until a web page fully loaded
This method return type is void so it returns nothing. This method returns the instance of the Navigation interface. This interface provides a lot of additional functionalities other than just loading Cookies.
To refresh the page you have to your pass URL again  To refresh the page this method provides a separate method navigate().refresh();
Navigation method can’t be used after the get method .
driver.get(URL);
driver.navigate().back();
Back button is not accessible here.
 
Browser commands can be use after Navigate().to() method 
Example : driver.navigate().to(URL);
driver.getTitle();
 

Let us consider a sample test script that will cover most of the Navigation Commands provided by WebDriver.

Selenium Webdriver Navigation Browser commands: Practice Example 3

Test Case 3: To perform Navigation commands

Steps:

  1. Lauch  google chrome browser.
  2. Navigate to “www.facebook.com”.
  3. Click on the link “Create a Page” and then Get Started button under Business or Brand
  4. Click on Back button for access Home page.
  5. Clcik on Forward button for access last open page
  6. Close the browser.

Test Scripts:

1. Launch the Google Chrome

WebDriver driver=new ChromeDriver();

2. Navigate to “www.facebook.com”

String url="https://www.facebook.com";
driver.navigate().to(url);

3. Click on the link “Create a Page”

driver.findElement(By.linkText("Create a Page")).click();
driver.findElement(By.xpath("//button[@data-testid='BUSINESS_SUPERCATEGORYSelectButton']/div/div[contains(text(),'Get Started')]"));

4. Click on Back button for access Home page.

driver.navigate().back();

5. Clcik on Forward button for access last open page

driver.navigate().forward();

6.Refresh the webpage

driver.navigate().refresh();

7. Close the browser.

driver.close();

How to run the  script:

  1. Write your complete script using Eclipse IDE
  2. Remove all compilation error if script having any error
  3. Right click on the script and Run as Java Application

Now you have understood all Browser and Navigation commands. WebElement commands I will cover in my next blog so keep reading my blog to get all complete knowledge.

Happy Learning…...

Read: Selenium & QTP Compared- What should you Know?


fbicons FaceBook twitterTwitter google+Google+ lingedinLinkedIn pinterest Pinterest emailEmail

     Logo

    JanBask Training

    A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.


  • fb-15
  • twitter-15
  • linkedin-15

Comments

Trending Courses

Cyber Security Course

Cyber Security

  • Introduction to cybersecurity
  • Cryptography and Secure Communication 
  • Cloud Computing Architectural Framework
  • Security Architectures and Models
Cyber Security Course

Upcoming Class

-1 day 29 Mar 2024

QA Course

QA

  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing
QA Course

Upcoming Class

-1 day 29 Mar 2024

Salesforce Course

Salesforce

  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL
Salesforce Course

Upcoming Class

6 days 05 Apr 2024

Business Analyst Course

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum
Business Analyst Course

Upcoming Class

-1 day 29 Mar 2024

MS SQL Server Course

MS SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design
MS SQL Server Course

Upcoming Class

6 days 05 Apr 2024

Data Science Course

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning
Data Science Course

Upcoming Class

-1 day 29 Mar 2024

DevOps Course

DevOps

  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing
DevOps Course

Upcoming Class

6 days 05 Apr 2024

Hadoop Course

Hadoop

  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation
Hadoop Course

Upcoming Class

-1 day 29 Mar 2024

Python Course

Python

  • Features of Python
  • Python Editors and IDEs
  • Data types and Variables
  • Python File Operation
Python Course

Upcoming Class

6 days 05 Apr 2024

Artificial Intelligence Course

Artificial Intelligence

  • Components of AI
  • Categories of Machine Learning
  • Recurrent Neural Networks
  • Recurrent Neural Networks
Artificial Intelligence Course

Upcoming Class

7 days 06 Apr 2024

Machine Learning Course

Machine Learning

  • Introduction to Machine Learning & Python
  • Machine Learning: Supervised Learning
  • Machine Learning: Unsupervised Learning
Machine Learning Course

Upcoming Class

20 days 19 Apr 2024

 Tableau Course

Tableau

  • Introduction to Tableau Desktop
  • Data Transformation Methods
  • Configuring tableau server
  • Integration with R & Hadoop
 Tableau Course

Upcoming Class

6 days 05 Apr 2024

Search Posts

Reset

Receive Latest Materials and Offers on Selenium Course

Interviews