Python script to open a website and login

Python script to open a website and login

Photo by Phillip Glickman on Unsplash

These steps should be able to be easily followed by even someone who hasn’t programmed anything in his/her life, since initially I have written them for my friend who has never done programming, but is just very enthusiastic about it. The python script described, is a simple, quick application to a real-life automation example, where one can see the results right away and feel like a boss while the computer clicks, opens and login to the websites instead of him/herself.

The repetitive task: Imagine yourself falling into a routine of opening and login to around 10 websites, that you are using and working with on a daily basis.

Automated solution: To automate this process I decided to use Selenium and Python in order to touch on and learn something new. (I have some Python knowledge, but I am completely new to Selenium). In the following steps I am going into such details that my non-programmer friend could be able to follow and set up this automation.

Assuming you have Python installed on your machine, let’s begin step by step:
(*I am using Python3)

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

Python script to open a website and login

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:

Python script to open a website and login

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:

  1. Open the website we are login to.
  2. 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.
  3. 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:

Python script to open a website and login

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!

Photo by Priscilla Du Preez on Unsplash

Hopefully, this will be found useful by more people rather than just my friend for whom I was initially showing this to. Feel free to ask me if you have any questions about this!

I wish you all the best and encourage you to keep experimenting with programming even if you are not a programmer, if you have not studied it or it’s not your major focus of interest in any way. Cheers!

How do I automatically login to a website using Python?

We will be using Selenium (python library) for making the auto-login bot..
First of all import the webdrivers from the selenium library..
Find the URL of the login page to which you want to logged in..
Provide the location executable chrome driver to selenium webdriver to access the chrome browser..

How do I open a website using Python code?

just open the python interpreter and type webbrowser. open('http://www.google.com') and see if it does what you want. yes. The result is same.

How do I create a login script in Python?

The Python script is as follows: print "Login Script" import getpass CorrectUsername = "Test" CorrectPassword = "TestPW" loop = 'true' while (loop == 'true'): username = raw_input("Please enter your username: ") if (username == CorrectUsername): loop1 = 'true' while (loop1 == 'true'): password = getpass.

How do I automate a website login?

Steps for Login Automation using Selenium WebDriver.
Create a Selenium WebDriver instance..
Configure browser if required..
Navigate to the required web page..
Locate the relevant web element..
Perform action on the web element..
Verify and validate the action..