Selenium Tutorial – Learn Selenium from Experts

Selenium Tutorial – Learn Selenium from Experts


Selenium is a popular open-source web-based automation tool. Selenium Webdriver is one of the most well known testing frameworks. It is an open source project that allows testers and developers alike to develop functional tests to drive the browser. Selenium Webdriver can work on any browser that supports JavaScript, since Selenium has been built using JavaScript.

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?

Selenium WebDriver is a web-based automation testing framework which can test web pages initiated on various web browsers and various operating systems. In fact, you also have the freedom to write test scripts in different programming languages like: Java, Perl, Python, Ruby, C#, PHP and JavaScript. Do note that Mozilla Firefox is Selenium WebDriver’s default browser.

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
  • Images
  • Hyperlinks
  • Radio buttons/ Check boxes
  • Text area/ Error messages
  • Drop down box/ List box/ Combo box
  • Web Table/ HTML Table
  • Frame

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

  • find_element_by_id
  • find_element_by_name
  • find_element_by_xpath
  • find_element_by_link_text
  • find_element_by_partial_link_text
  • find_element_by_tag_name
  • find_element_by_class_name
  • find_element_by_css_selector

To find multiple elements (these methods will return a list:

  • find_elements_by_name
  • find_elements_by_xpath
  • find_elements_by_link_text
  • find_elements_by_partial_link_text
  • find_elements_by_tag_name
  • find_elements_by_class_name
  • find_elements_by_css_selector

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()

driver.get(“https://www.facebook.com/”)
print(driver.title)
driver.close()


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.

driver.get(“https://mildaintrainings.com/selenium-testing-training/”);
driver.refresh();
driver.back();


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 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.


driver. find_element_by_name(“email”) . sendKeys(“xxx@example.com”)
driver. find_element_by_name(“pass”) . sendKeys(“xxxxxx”)
driver. find_element_by_id(“u_0_q”) . click()


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.


Drop us a Query

About the author

Bhaskar

Bhaskar

The author has a keen interest in exploring the latest technologies such as AI, ML, Data Science, Cyber Security, Guidewire, Anaplan, Java, Python, Web Designing tools, Web Development Technologies, Mobile Apps, and whatnot. He bags over a decade of experience in writing technical content.

Corporate
whatsapp arrow
Loading...
// load third party scripts onload