Most of the big-name browser vendors support it. And many of these either have taken or are taking steps to make Selenium a native component of their browsers. Additionally, it’s the core technology used in a variety of other browser automation tools, APIs, and frameworks.
Selenium runs on many browsers and operating systems, and a variety of programming languages and testing frameworks can control it. Also, Selenium can automate tedious web-based administration tasks.
What Is Selenium WebDriver?
WebDriver was introduced as part of Selenium v2.0. Selenium v1 consisted of only IDE, RC and Grid. But the major breakthrough in the Selenium project was when WebDriver was developed and introduced as a replacement in Selenium v2. However, with the release of Selenium v3, RC has been deprecated and moved to legacy package. You can still download and work with RC but, don’t expect any support for it.
You must have heard the term “browser elements” a number of times. The next part of this Selenium tutorial will be about what are these elements and how testing happens on these web elements.
What Are Browser Elements?
Elements are the different components that are present on web pages. The most common elements we notice while browsing are:
- Text boxes
- CTA Buttons
- Radio buttons/ Check boxes
- Text area/ Error messages
- Drop down box/ List box/ Combo box
- Web Table/ HTML Table
Testing these elements essentially means we have to check whether they are working fine and responding the way we want it to. For example, if we are testing text boxes, what would you test it for?
1. Whether we are able to send text or numbers to the text box
2. Can we retrieve text that has been passed to the text box, etc.
If we are testing an image, we might want to:
1. Download the image
2. Upload the image
3. lick on the image link
4. Retrieve the image title, etc.
Similarly, operations can be performed on each of the elements mentioned earlier. But only after the elements are located on the web page, we can perform operations and start testing them right? So, the next topic, I will be covering in this Selenium tutorial blog is element locator techniques.
Locating Browser Elements Present On The Web Page
Every element on a web page will have attributes (properties). Elements can have more than one attribute and most of these attributes will be unique for different elements. For example, consider 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 unique for each element. In other words, two elements cannot have the same attribute value. Elements can have the same value for ‘Class Name’.
In the example considered, the image and text box can neither have the same ‘ID’ value nor the same ‘Name’ value. However, there are some attributes that can be common for a group of elements on the page. I will tell you which are those attributes later, but before that let me list down the 8 attributes using which we can locate elements. Those attributes are ID, Name, Class Name, Tag Name, Link Text, Partial Link Text, CSS and XPath.
Since the elements are located using these attributes, we refer to them as ‘Locators’. The locators are:
To find multiple elements (these methods will return a list:
By looking at the syntax above, you might have realized locators are called inside methods. So, before going any further, you need to learn all the other methods, browser commands and functions that can be used to perform operations on the elements.
Operations On Browser Elements
To start testing a web page, we need to first open a browser, then navigate to the web page by providing the URL right? Check out the below piece of code, where I have replicated the same. Firefox browser will first be initiated and then it will navigate to Facebook’s login page.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
from selenium import webdriver is a library package which contains the required class to initiate the browser loaded with a specific driver.
driver = webdriver.Firefox() This command is used to initiate a new Firefox driver object.
driver.get(“https://www.facebook.com/”) This method is used to open the specified URL.
driver.title This command gets the title of the tab that is currently open in the browser.
driver.quit() This command closes the browser driver.
But, what if you want to navigate to a different URL and then do testing? In that case you can use the driver.get() command as shown in the below code snippet. If you then want to come back to the previous page, then you can do that by using driver.back() command. Similarly for refreshing the current page, you can use driver.refresh() command.
Now that you know most of the basics, let’s go to the next topic in this Selenium tutorial blog. Let’s try to find an element on the web page and then perform any operation that is possible.
I’m pretty sure, you all have Facebook accounts. So, let me show you how to log into Facebook by passing the credentials from the code itself.
There are two text fields in the Facebook login page, one for Email/Phone and another for Password. We have to locate these two elements, pass the credentials to those elements and then find the third element: Login button which needs to be clicked on.
If you can recall, I mentioned earlier that these elements can be located using element locator techniques. Let’s use it to locate these elements and send the field values.
This is the syntax for finding the element: driver.findElement(By.id(“xxx”));
For sending it values, we can use the method sendKeys(“credentials“);
For clicking on a button, we have to use the method click();
So, let’s get started with finding the element and performing an operation on it. The code for it is in the below snippet.
In line #1, we are identifying the Email element by its unique ‘Name’ attribute and sending it the EmailID.
In line #2, we are identifying the Password element by its unique ‘Name’ attribute and sending it the password.
In line #3, we are locating the Login button element by its unique ID and clicking on that button.
Voila! You have successfully logged in, which means your complete code executed completely.
I have used the ID and Name attributes for locating elements. You can in fact use any other locator for finding the elements. XPath is the most useful and important of the locator techniques. But, as long as you can find even one of the attributes and use them for locating elements, you should be good.
If you wish to learn Selenium and build a career in the testing domain, then check out our interactive, one to one online Selenium Certification Training program here, that comes with 24*7 support to guide you throughout your learning period.