Introduction to Selenium

Selenium is a free (open-source) automated testing suite for web applications, which supports cross-browser and cross-operating-system interoperability. It is quite similar to HP’s Quick Test Pro (QTP, now UFT), but Selenium focuses only on automating web-based applications. Testing done using the Selenium tool is usually referred to as Selenium testing.

It is useful for testing web applications only. Neither desktop (software) testing nor the testing of mobile applications is possible with Selenium.

Read more of this Selenium testing tutorial to get in-depth knowledge of automation testing.

Watch this Selenium Tutorial for Beginners

Selenium Tutorial for Beginners on WebDriver Using Python


A web application is an application program stored on a remote server, which is allowed to get accessed through a web browser over the Internet. Many websites contain web applications. Any website component that performs functions for users qualifies as a web application. Few examples of web applications are:

  • Google Search Engine
  • AliExpress

Coming back, to test the performance of these web applications, companies carry out the testing process. The testing of web applications done through the Selenium tool is referred to as Selenium testing.

Selenium Types

Selenium is not just a single tool but a suite of software, each catering to different testing needs of an organization. It has four components:

  • Selenium Integrated Development Environment (IDE)
  • Selenium Remote Control (RC)
  • Selenium Grid
  • Selenium WebDriver

Now that you know about its types, let’s talk about each one of them briefly.

Selenium IDE

Selenium IDE (Integrated Development Environment) is a tool that helps you develop your test cases. It is an easy-to-use Chrome and Firefox extension and is generally the most reliable method to develop test cases. It records users’ actions in the browser for you, using the existing Selenium commands with parameters defined by the context of the web element. This is not only a time-saver but also an excellent way of learning Selenium script syntax. Previously known as Selenium Recorder, Selenium IDE was initially created by Shinya Kasatani of Japan and was contributed to the Selenium project in 2006.

It was introduced as a Firefox plugin for the faster creation of test cases. As it was a Firefox extension, it could automate the browser through a record-and-play feature providing auto-completion support and the ability to move commands around quickly.

Scripts are recorded in a special test scripting language called Selenese for Selenium. Selenese comes up with commands for carrying out actions in a web browser and restoring data from the resulting pages.

The advantage of Selenium IDE is that the tests recorded via the plugin can be exported in different programming languages, such as Java, Ruby, Python, etc.

Selenium17

Selenium RC (Remote Control)

Selenium Core was the first version, but with that version, testers had to install both Selenium (a JavaScript program) and the webserver containing the web application being tested on their local systems so that they would belong to the same domain.

Then, a ThoughtWorks’ Engineer, Paul Hammant decided to create a server that will act as an HTTP proxy to trick the browser into believing that Selenium Core and the web application being tested belong to the same domain, thus making RC a two-component tool.

  • Selenium RC Server
  • Selenium RC Client (the library containing the programming language code)

RC can support the following programming languages:

  • Java
  • C#
  • PHP
  • Python
  • Perl
  • Ruby

Selenium2

Become a Test Architect

Selenium Grid

Selenium Grid is a testing tool that lets you run your tests on various machines against different browsers. It is part of the Selenium suite that specializes in running multiple tests across different browsers, operating systems, and machines. You can connect to it with Selenium Remote Control by stating the browser version, browser, and operating system as per your choice. You will be able to specify these values through Selenium Remote Control capabilities. With Selenium Grid, one server makes a move as a hub. Tests communicate to the hub to get access to browser instances. The hub has a list of servers that provide access to browser instances (WebDriver nodes) and lets tests use these instances.

Selenium Grid allows parallel testing and also managing different browser versions and browser configurations centrally (instead of in each individual test).

There are multiple online platforms that provide an online Selenium Grid that you can access to run your Selenium automation scripts. For example, you can use LambdaTest.

Selenium Grid has more than 2,000 browser environments over which you can run your tests and truly automate cross-browser testing.

Selenium3

Selenium WebDriver

It was founded by Simon Stewart, a ThoughtWorks’ Consultant in Australia, in 2006. Selenium WebDriver was the first cross-platform testing framework that could control the browser at the OS level. Selenium WebDriver is a successor to Selenium RC. Selenium WebDriver accepts commands (sent in Selenium or via a client API) and sends them to the browser.

This is implemented through a browser-specific driver, which sends commands to the browser and retrieves the results. Each driver launches and accesses a browser application. Different WebDrivers are:

  • FirefoxDriver (GeckoDriver)
  • ChromeDriver
  • InternetExplorerDriver
  • MicroEdge
  • OperaDriver
  • SafariDriver
  • HTMLUnitDriver

To extensively understand the working of WebDriver and its benefit when used with Selenium, watch the below Selenium WebDriver tutorial video:

Selenium Tutorial for Beginners on WebDriver Using Python

Benefits of Selenium WebDriver

  • Selenium WebDriver supports seven programming languages: Java, C#, PHP, Ruby, Perl, Python, and .Net.
  • It supports cross-browser interoperability that helps you perform testing on various browsers, namely, Firefox, Chrome, IE, Safari, etc.
  • Tests can be performed on different operating systems: Windows, Mac, Linux, Android, and iOS.
  • Selenium WebDriver overcomes limitations of Selenium v1, such as file upload, download, pop-ups, and dialog barrier

Cons of Selenium WebDriver

  • Detailed test reports cannot be generated.
  • Testing images is not possible.

No matter what, these shortcomings can be overcome by integrations with other frameworks. That is, for testing images, Sikuli can be used, and for generating detailed test reports, TestNG can be used.

Now, you know what Selenium is and have a fair idea about the various tools of the Selenium suite.

Next, you will learn all that you need to know to get started with testing web apps using Selenium WebDriver. The below image depicts how WebDriver works:

Selenium4

What is Selenium WebDriver?

In this Selenium WebDriver tutorial, let’s dig deeper into Selenium WebDriver. Let’s understand more about what Selenium WebDriver is, what browser elements are, and how to locate browser elements on a web page.

Locating and testing of web elements in the web application is implemented through a browser-specific driver. It controls the browser by directly communicating with it.

In Selenium WebDriver, you have the liberty to write test scripts in different programming languages, such as Java, Perl, Python, Ruby, C#, PHP, and JavaScript. However, make a note that Mozilla Firefox is Selenium WebDriver’s default browser.

WebDriver was introduced as part of Selenium v2.0. Selenium v1.0 consisted of only IDE, RC, and Grid. The major breakthrough happened in the Selenium project when WebDriver was developed and introduced as an addition to Selenium v2. With the release of Selenium v3, RC was deprecated and moved to a legacy package. Although you can still download WebDriver and carry out tasks with RC. There would not be any support for it in Selenium v3.

In a nutshell, the advantages WebDriver has over RC are:

  • Support for more programming languages, operating systems, and web browsers
  • Ability to overcome the limitations of Selenium v1
  • Simpler commands when compared to RC and a better API
  • Support for batch testing, cross-browser testing, and data-driven testing

The drawback WebDriver has over RC is that test reports cannot be generated in WebDriver; whereas, RC generates detailed reports. You must have heard the term ‘browser elements’ a number of times. The next part of this automation testing tutorial for beginners will be about browser elements, and you will see how testing happens on these web elements.

What are browser elements?

Browser elements are different components present on web pages. The most common elements you will notice while browsing are:

  • Text boxes
  • CTA buttons
  • Images
  • Hyperlinks
  • Radio buttons/checkboxes
  • Text area/error messages
  • Dropdown box/list box/combo box
  • Web table/HTML table
  • Frame, etc.

Testing these elements essentially means, you have to check whether they are working fine and responding the way you want them to. For example, if you are testing a text box, what would you test it for?

1. Whether you are able to send text or numbers to the text box
2. If you can retrieve the text that has been passed to the text box, etc.

If you are testing an image, you might want to:

  • Download the image
  • Upload the image
  • Click on the image link
  • Retrieve the image title, etc.

Similarly, operations can be performed on each of the elements mentioned earlier. However, only after the elements are located on the web page, you can perform operations on them and start testing them.

Watch this video on Selenium Tutorial for Beginners:

Selenium Tutorial for Beginners on WebDriver Using Python

Locating Browser Elements Present on a Web Page

Each element on a web page does have an attribute (property). Elements can have multiple attributes, and most of these attributes will be distinctive for different elements. Consider an example. Suppose there is a page having two elements: an image and a text box. Both these elements have a ‘Name’ attribute and an ‘ID’ attribute. These attribute values need to be distinctive for each of these elements. In other words, no two elements can have the same attribute value.

In the above example, the image and the text box can have neither the same ‘ID’ nor the same ‘Name’ value. However, there are some attributes that can be common for a group of elements on the page, e.g., a group of elements can have the same value for ‘Class Name’.

There are eight attributes that can be used to locate elements on a web page; they are ID, Name, Class Name, Tag Name, Link Text, Partial Link Text, CSS, and XPath. Since the elements are located using these attributes, they are referred to as ‘locators’. The locators are:

By.id

driver.findElement(By.id(“xxx”));

By.name

driver.findElement(By.name(“xxx”));

By.className

driver.findElement(By.className(“xxx”));

By.tagName

driver.findElement(By.tagName(“xxx”));

By.linkText

driver.findElement(By.linkText(“xxx”));

By.partialLinkText

driver.findElement(By.partialLinkText(“xxx”));

By.css

driver.findElement(By.css(“xxx”));

By.xpath

driver.findElement(By.xpath(“xxx”));

By looking at the syntax above, you might have realized why locators are called inside methods.

Now, you need to learn all the other methods, browser commands, and functions that can be used to perform operations on the elements. But before moving on with the hands-on, creating an automated test, let’s first understand what dependencies are and how they help you in creating a Maven project.

You will need certain dependencies and libraries ready with you for the Selenium project, which will help you perform automated testing of a web application, and such a tool is known as Maven.

What is Maven?

Maven is a build automation tool used primarily for Java projects by downloading its dependencies.

Basically, Maven is a software that helps you download dependencies for a software program. When you create a Selenium project, you need to specify all Selenium components that are required to be included inside a POM file for the Selenium project to be ready. Once the dependencies are added to the POM file, you can simply save the project, and all these dependencies will automatically be downloaded.

Setting up Selenium with Maven and TestNG on Eclipse

Before diving into this section of the tutorial, let’s see how Eclipse projects run. For making Eclipse Java projects run, you need a library that can produce an HTML report of execution and display the test case that has failed. It is done by the TestNG library. When bugs are accurately located like this, they can be fixed immediately to the relief of developers.

TestNG is a testing framework. It structures, groups, and launches tests. It also generates testing reports. To get a function executed, you need to include the @Test annotation before the definition of that function.

When you run this file as TestNG suite, the execution will start, and you will get the detailed test reports. You will get the test output in your console tab and the result of the test suite in the next tab.

You will use TestNG for several reasons:
1. TestNG annotations are easy-to-create test cases.
2. Test cases can be grouped and prioritized more easily.
3. TestNG supports parameterization.
4. It also supports data-driven testing using data providers.
5. It generates HTML reports.
6. Parallel test execution is possible.
7. TestNG readily supports integration with other tools and plugins like Eclipse IDE and build tools like ANT, Maven, etc.

Now, this tutorial will move on with the hands-on part. This section is divided into three parts:

  • Java JDK Installation
  • Eclipse Installation
  • Performing a Selenium Test Case

Installing Java JDK

Step 1: Download Java JDK from the link provided below, and then click on the Oracle JDK Download button
https://www.oracle.com/technetwork/java/javase/downloads/index.html


Click on the radio button, Accept License Agreement, and download the .exe file related to your OS type

Selenium8
Step 2: Open the downloaded execution file, and click on Next

Selenium9
Step 3: Select Developers Tools and click on Next, and you will be directed to the below screen. It will take a few moments to set up JDK in your system


Step 4: Click on Close to complete the setup

Selenium11
Before moving ahead, you need to add the environment variables to the path as follows.

Step 5: Go to local C drive > Program Files > Java > jdk-12.0.1 > bin

Selenium12
Step 6: Copy the path as shown above
Step 7: Go to Control Panel > System and Security > System > Advanced system settings > System Properties


Step 8: Click on Environment Variables as shown above and the below window pops up in which you will perform the following steps:

1. Click on Path in the System variables section, under Environment Variables
2. Click on Edit

Selenium14
Step 9:
1. Click on Browse and find your file path as local C drive > Program Files > Java > jdk-12.0.1
Keep clicking ‘OK’
2. Now, you have Java JDK installed in your system

Installing Eclipse

Step 1: Go to the Eclipse download page (the recent one is Eclipse Installer 2019-06 R) through this link: https://www.eclipse.org/downloads/

Selenium16
 Step 2: Click on Download 64 bit. You would land on a page as shown below:

Selenium18
Step 3: Once the download is finished, launch the installer
Step 4: Click on Eclipse IDE for Enterprise Java Developers

Selenium19
Step 5: Once done, click on INSTALL

Selenium20
Step 6: Then, click on Launch

Selenium21
Now that you have successfully set up Java and Eclipse in your environment, let’s perform a Selenium Test Case using Maven.

Performing a Selenium Test Case

Step 1: Start a new Maven Project


Step 2: Check the Create a simple project checkbox and click on Next

Selenium24
Step 3: Enter Group Id and Artifact Id and click on Finish
Group Id: Group Id is the Id of the project’s group. Generally, it is unique across an organization.
Artifact Id: Artifact Id is the Id of the project. It specifies the name of the project.

Selenium25
Step 4: Now, your project would appear in the Project Explorer section as shown below:

Selenium26
Before moving on to the scripting part, you need to configure the Maven dependencies to perform the Selenium test case in Eclipse. You will be adding the Maven dependencies to the pom.xml file under the target folder.
Step 5: Now, go ahead and add the below dependencies to the pom.xml file
Note: Copy the code from below and paste it after the JUnit dependency, which will be already present in your .xml file

<dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
</dependency>
 
<dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>3.141.59</version>
</dependency>
 
<dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-api</artifactId>
            <version>3.141.59</version>
</dependency>
<dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>3.141.59</version>
</dependency>

Selenium27
Step 6:
1. Right-click on the file name
2. Click on Properties

Selenium28
Step 7:
1. Select Java Build Path
2. Click on Libraries
3. Click on Add Library
Selenium29
Step 8:
1. Click on TestNG
2. Press Next, and then Finish

Selenium30
Step 9: Click on the Apply and Close button
Step 10: Go to www.facebook.com, right-click on the page, and click on Inspect

Selenium31

Step 11: Follow the process shown below and copy the XPath of the email:

  1. You will use this arrow to select an element in the page to inspect it
    2. Select the email web element, which will point to the section of this web-element’s HTML code
    3. Click on Chropath; this helps you in getting XPaths and CSS selectors for the web elements of a web page, uniquely
    4. Copy the RelXpath. XPath is defined as an XML path. It is a syntax or language for locating any element on the net page using XML path expression

Selenium32
Step 12:
1. Paste the xpath in the xpath expression (i.e., key)
2. Enter the value (email ID)

Selenium33
Step 13: Do the same for password xpath

Selenium34
Step 14: Do the same for login xpath as well

Selenium35
Step 15: Your code would look like this:

@Test
public class App
    
{           
 WebDriver driver;
            
            public void test()
            {
            System.setProperty("webdriver.chrome.driver","C:\\Users\\intellipaat\\Desktop\\chromedriver.exe");
      
      
                          driver = new ChromeDriver();
                         //The website name you want to visit
                         driver.get("http://www.facebook.com");
      driver.findElement(By.xpath("//input[@id='email']")).sendKeys("[email protected]");
      driver.findElement(By.xpath("//input[@id='pass']")).sendKeys("helloworld123");
                         driver.findElement(By.xpath("//input[@id='u_0_2']")).click();
              }
}

Note: Here, you must provide a valid email ID and password. In this example, a dummy ID and password would be used.

Selenium36
Step 16:
1. Right-click on the screen and scroll down to Run As
2. Click on 1 TestNG Test


Step 17:
1. Once done, the Chrome browser window will pop up
2. It will search for facebook.com
3. Then, it will automatically type the email
4. It will automatically type the password as well
5. It will then click on Log in and navigate you to your FB homepage

Selenium37
Step 18: You’ll be navigated to your FB account
Selenium38
Step 19: Go to Eclipse IDE again and see the console. You would see the Test report with the results

Selenium39
That’s it! You have successfully performed a Selenium test case.

Creating Automated Tests

There are three steps to execute automated tests:

  • Find an element on the web browser
  • Perform an action on that element
  • Test and create a test report with the results

Selenium40

Finding an Element on the Web Browser

An element on a web browser can be found using:

  • ID
  • Name
  • Class Name
  • Tag Name

Performing an Action on That Element

The next step is performing an action. For that, you can try the following options:

  • Click(): Used to click on a web element
  • sendKey(): Used to take values into text boxes
  • Clear(): Used to clear textboxes of its current value
  • Submit(): WebDriver will automatically trigger the submit function of the web application where that element belongs to

Testing and Reporting

Report generation is extremely vital once you do the automation testing, likewise for manual testing. By looking at the result, you can easily identify how many test cases are passed, failed, and skipped. By viewing the report, you will come back to understand what the status of the project is.

Selenium Internet Driver is employed for automating the web application. However, it will not generate any reports. TestNG will generate the default report.

Selenium Career Opportunities

As numerous companies have started using web applications, the need for Selenium is shooting up. It has grown tremendously and has become one of the most integral parts of various businesses. When it comes to web testing tools, Selenium has surely become one of the best in the industry to help you with automation testing services.

There are various Selenium career opportunities for a tester. Below mentioned are some of the popular job roles you can consider to work as a Selenium WebDriver professional:

  • Automation Test Lead
  • Senior Test Engineer
  • Quality Engineer
  • Selenium Automation Analyst
  • QA Engineer

The most interesting part here is that the industry has experienced a humongous surge in creating job opportunities with exciting packages. So, you must take the assistance from experts and get yourself qualified enough to make the best of these opportunities.

Are you interested? Check out Intellipaat’s Selenium Training – WebDriver Course and enroll now!

Frequently Asked Questions

What is a Selenium test tool, and how does it work?

Selenium, a free automated suite of software for testing web applications, offers a playback/record tool for writing tests without having to learn the test scripting language. When you learn Selenium, it enables you to automate your web application for testing.

How Selenium is better than other testing tools?

Selenium is an open-source software that anyone can download and use for free. It is used to automate web browser and supports a variety of programming languages to write test scripts. It also supports cross-browser testing. Moreover, you can easily integrate Selenium with other tools. It supports parallel and distributed testing and has no dependency on GUI-based systems.

Why learn Selenium?

The industry’s demand for test automation continues to grow, and Selenium is the world’s most popular test automation tool. The average salary of a Selenium Automation Engineer is US$81,405 (PayScale) in the United States and ₹549,436 (Glassdoor) in India. Thus, as a professional looking to make a mark in the industry, you must learn Selenium.

Is it easy to learn Selenium?

To learn Selenium, you need to know the basics. Knowledge of the OOPS concept is vital. This Selenium tutorial will provide you with valuable insights that will help you learn Selenium, its installation, features, and more.

Although the tools help, they will not give you any knowledge. So, if you want to learn Selenium basics, start with learning test automation first.

Can I learn Selenium without knowing Java?

You don’t have to know all the features of Java because it is not required for automated testing of Selenium. Even as a non-programmer, you can learn a lot from this Selenium Java tutorial. However, for the most commonly used Selenium WebDrivers, you need Selenium basics such as OOP.

What is Selenium used for?

This is mainly used to test web applications. It also provides a domain-specific language for writing test cases in a common programming language, such as Scala, Java, C#, Ruby, and more. These automated test tools are used by most companies for their products.

What are the different components of Selenium?

Selenium has four components, including Selenium Remote Control (RC), Integrated Development Environment (IDE), WebDriver, and Selenium Grid. The last two are the most famous. Each has its own unique features, including support for multiple browsers, parallel testing, and more. Intellipaat’s Selenium tutorial will help you learn Selenium and the details about its all components.

Which programming language is best for Selenium?

Learners who are not comfortable with programming can use Python for its script-friendly feature or Ruby. This Selenium Python tutorial will improve your learning curve and help you understand how to write software tests with fewer codes.

How to use Selenium Python?

Follow the below steps to configure Selenium using Python:

  • Download and install Python on Windows
  • Install Selenium libraries in Python
  • Download and install PyCharm
  • Create a new project and write the Selenium test script
  • Run and validate the test scripts.

Also, check out this in-depth video on Selenium Python tutorial:

Selenium Tutorial for Beginners on WebDriver Using Python

Youtube subscribe

Recommended Videos

Course Schedule

Name Date
Test Architect 2021-04-10 2021-04-11
(Sat-Sun) Weekend batch
View Details
Test Architect 2021-04-17 2021-04-18
(Sat-Sun) Weekend batch
View Details
Test Architect 2021-04-24 2021-04-25
(Sat-Sun) Weekend batch
View Details

3 thoughts on “Selenium Tutorial for Beginners on WebDriver Using Python”

Leave a Reply

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