How to automate opening and login to websites with Python
By: Hristina Gulabovska
Tags: Selenium, Programming, Python, Selenium Webdriver, Beginners Guide

Create new folder

We want to make sure that all of the required stuff (the python script, the Chrome driver and as an extra security the yaml file for hiding the passwords from our main script) are put together in one place so they can “see each other, communicate and cooperate” :)

I will call this folder website_login.

2. Download ChromeDriver

Go to this link to download the ChromeDriver. Click on the given “Latest stable release” and choose the zip file that is applicable to your system. (If you have 64-bit Windows, no worries that you probably can not see an option for that, go ahead and download the chromedriver_win32.zip file)

After downloading, extract the zip folder and copy the chromedriver.exe file to the folder we created in the step 1. We will need this piece to be able to manipulate the Chrome browser from our python script.

3. Install Selenium library for Python

Next, we need to install the Selenium library for Python, which allow as to automate the browser through our python script. To do that, open the Command Prompt (cmd) and type the below command:

pip install Selenium

Screenshot of installing Selenium using the command

4. Creating the python script and yaml file

You can use a simple notepad application for this. Create a new file and save it in the folder we created, giving the .py extension. For example I am calling mine: WebsitesLoginAutomation.py

For the purpose of keeping the password hidden from the reader of the main python script, create one more file using the notepad and save it with the .yml extension. Here we will store the passwords and use them in our main python script with general name, so that if you want to show someone your python script, he/she can not glance at your secrete password. I am naming my .yml file: loginDetails.yml

Here is the final structure of our folder. Yours should look similar to this:

Structure of our folder created in step 1.

Now, let’s just create the content of the WebsiteLoginAutomation.py and loginDetails.yml files.

5. Writing the python script and yaml file

For the purpose of this example, I decided to write a script that will automatically log me in to my Facebook account.

Let’s quickly finish the yaml file first, just to store the original username and password that we are going to use in our python script.

That’s all you need in the yaml file. Now, the python script.

On the first two lines of the .py file, we will import the Selenium library that we downloaded to be able to use it as well as we will import yaml to be able to use the login details from the yaml file we just finished.

And on the next lines we are write code that takes the login details from the yml file and stores tham into new variables. We will use our login details through out the python script using this newly created variables, in order to hide our original password from the eye of anyone viewing our python script.

Up until now, the python file should look like this:

Next, we set up the chrome driver which helps us to touch the browser. After we have the Chrome driver, we create our python function that uses the driver to:

Open the website we are login to. Finds the fields on the website where it needs to put our username, password and the field where the login button is put. This is probably othe hardest part of this whole process, since we have to give the script the name or ids of those fields. We will do this by going to the website and inspect it. No worries, I will explain this in more details in the next step 5. Once we do it, it is not so big of a deal as it maybe sounds. After we have that, the function is able to put the login data in the relating fields and clicks the button.

So this is the code that you used put it next:

The final line of code we need is the one that will call our function with the specific details (the specific website, passwords…) that we want to use it.

5. Explain the web scraping part

As I said, let’s look at the web scraping part of our task, then identify what goes where in our python code, and finally move to step 6 where we will run the example and as a result enjoy how our computer automatically opens and logs in to the website.

In our python function, using the driver, we are looking for the elements on the website by their element id. Keeping this in mind, we go to our website (in this example facebook.com), right click on the email field and choose the “Inspect” from the menu. Then find the id of the field that you clicked on, you will need that to put it in the python script. I know I sound confusing, so please take a look at the gif below to get a better understanding of what I am trying to explain.

In a similar way we are getting the id of the password field, and the id of the login button. Then in the python code, where we are calling the function, this is where we put the id-s of the fields we need:

Here is the line where we pass on the id-s to our function call

6. Run it!

Ok! Let’s see this working!

Open Command Prompt and go to the website_login folder

Run the script using this command

python WebsitesLoginAutomation.py

Boom! Enjoy the result looking at the computer doing the job instead of you!