Automate LinkedIn Login with Selenium

Chamo Wijetunga
4 min readJun 24, 2020

--

In our previous post we understood what web automation is. So here, I am going to demonstrate how to automate LinkedIn login and go to notification page by using selenium web automation and Firefox browser.

LinkedIn Automation

First step, Download and install Python

Before start automating we must have to download python current version to our computer. First of all lets check we have installed it previously by running python on command prompt.

Python is already installed.

I have installed it previously. If not go to python official page and select the most newest version and download it, when downloading and installing remember to tic the line that says add python to the path variable.

Must add python to the path

Then we have to download selenium

Downloading selenium is a very simple process. You just have to run the command pip install selenium in the command prompt as an administrator. It will install the all necessary libraries and add selenium to out system.

Lets move on, Web Drivers.

The processes is very easy if you use chrome as the web browser. But i am a Firefox guy. So i have to download the Gecko drivers for Firefox from the GitHub repo.

Download the latest version for windows.

Download gecko drivers for windows

This is the thing, Gecko driver is a simple cmd file and it is executable. when we write the automation code that executable file must be inside the main folder that we are working on. See, Simple. No installations, No commands. Just have to keep the fie on the working directory.

So Let us begin

Before that working IDE, I am using visual code as my python IDE. You can use any IDE as you wish. First of all we have to import selenium web driver.

Fig 1

After importing the web driver and selenium library we have to define the browser drivers in third line in fig.1 I did it by setting the browser web driver variable assigning it to Firefox web drivers.

Then i use the get( ) command. This command will fetch the data from the given parameters and use it a s a URL in the browser. Then lets try to run this. simply by python [FileName] in cmd or terminal on the visual code.

Automated web page

Yeah, Its working. Then we have to go to sign in button and click it. There is a magical way of doing this on selenium. First of all we have to inspect the elements on the sign in button and copy the xpath value to our clipboard.

Copying the xpath

Then we have to take the xpath to identify by our selenium drivers.

Fig 2

In line number seven I crated a variable called signInButton in the code. I passed driver.find_element_by_xpath command and gave the copied xpath of our sign in button. Then browser will automatically find out what button they want to find. After finding it I just passed a click( ) command to click on the browser, and the sign in button will be clicked then a forum to input email and password will be opened.

Fig 3

Username and password input fields will be displayed. Same as the last time we have to get the xpath of those two areas and set it to a variable. email and password. The new thing here is typing something automatically in the fields. To do that we have the send_keys command with passing the variable. easy and simple. Here I cleared my password field. ;)

Fig 4

Then we have to login. Login is a button with a xpath value on it. So e have to get the login button xpath and pass it on a variable and click( ) on it. Them After login part is done I got the xpath value for notification button and passed it on a variable. So my whole script will automatically login to the LinkedIn account and check what are the notifications are.

Thats all from my side, Peace !!

--

--