In this tutorial, we are going to learn browser automation using the Selenium library in Python. 
 
Let’s say there are tasks that we constantly repeat in daily life. It will take our time to do all these repetitive tasks manually. For this reason, using the Selenium library in Python, we can perform repetitive tasks with a few lines of code very quickly and in one go.
 
 

What is Selenium

Selenium is a web testing tool which uses simple scripts to run tests directly within a browser. 
 
Seleniumis functional for all browsers and its scripts are written in varius programming languages. ( Python, Java, C# etc. ) 
 
 
In this tutorial, we are going to learn browser automation with Selenium through an example.
 
In this example, we will automate the login to the GitHub. For this, we need to do:
 

Installation

Step 1)  First, we need to download the Selenium library. ( In this example we are going to use Pycharm IDE )
 
Click File and then Settings.
 
 
 
 
Step 2 ) In the new Window, click Project and then Project Interpreter. Then click the  icon.

 

Step 3) In the new window, type selenium, and then click Install Package

 

Step 4 ) To automate the browser we need to download webdriver. To do that, go to pypi.org/project/selenium and then scroll down and find Drivers.  Choose the browser you use. ( We use Chrome Browser. ) 

 

Step 5 ) In the new window, choose the version of the browser you are using.

 
Step 6 ) In the new window, download the driver. And then extract the zip file somewhere on your computer. 

Automate the Github

 
Step 1 ) First, we are going to import webdriver. For this, we enter the computer path that we downloaded and extracted the webdriver.

 

 
Step 2 ) After importing the webdriver,  we are going to go to github.com . To do that we use the get () method. 

 

When we run the program, browser opens and we go to github.com .  
 

 

 

Step 3 ) Now we have to sign in the Github. To do this, we have to tell Selenium to find the “Sign in” element and click it. We have several ways to find any element. For example id, text link, class, name, etc. 
 
To do this, let’s right-click on the “Sign in” button and then inspect it.
 
As you can see there is no id. class names are generic and can be reused anywhere. Therefore, the only way we can find the “Sign in” element is its text.

 

 

Step 4 )  We are going to find the Sign in element by its text.  To do that we use find_element_by_link_text to get the element. Then we use the click () method to click it. 
 
When we run the program, we go to github.com and then login page automatically.

 

 

Step 5 ) Now we have to write our username and password . We are going to do the same thing here. Lets inspect them and find how we can find their elements. 
 
 
As you can see we can find the username or email address field by its id. ( login_field ) .

 

We use the send_keys () method to fill out the field.

 

 

We can find the password element by its id. ( password )

 

Let’s do the same task for password element.

 

We use the submit () method to submit it.

 

As you can see that we signed in.

 

 

Step 6 ) Finally, after doing all the operations, we must end the process with the quit () method. 

 

As you can see, we were able to automate a browser with a few lines of code. By using all these in the class, you can test and automation processes in a more organized way. If you want to learn more, you can go to https://selenium-python.readthedocs.io/.

Leave a Reply

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