I have made a YouTube video with voiceover showing these steps here: https://www.youtube.com/watch?v=t44Xt1vWNxU
This tutorial I made on Windows, but you can apply this technique on any OS.
Selenium is often used for automated testing in the browser, but can be used for automating all kinds of things in the browser. If what you’re automating involves logging into an account, this shows how to load your login cookie so that when you launch the browser again, you will already be logged in when you go to your site.
You must have python installed.
You can do:
$ python --versionPython 3.7.4
To see if you have python installed. I am using 3.7.4 at the time of this writing.
You must have selenium installed.
You can do:
$ pip list | grep seleniumselenium 3.141.0
in Git Bash (this is the terminal I like to use so that I can get bash like behavior on Windows, feel free to use whatever console you like)
You can see from my output that I am using selenium 3.141.0 at the time of this writing.
If you do not have selenium you can install it using
pip install selenium
You can check to see if you have pip like this
py -m pip --version
and if you do not have pip you can install it like this:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
This is just downloading and running a script called get-pip.py and is my preferred method. I have never had issues using this method to install pip.
Let’s get Started
- Create an empty folder called UserData. You can put it anywhere you like. You are going to encode the path to this folder in your script.
- In your Chrome browser go to Hamburger Menu -> Help -> About Google Chrome
The hamburger menu is the button with the 3 dots or often three lines or slashes.
- Now to go the ChromeDriver download page and download the Chrome driver closest to your version of Chrome. If it is not an exact match that is fine, the minor version numbers are just for minor patches and bug fixes, its just important that the major version numbers match.
You can see here what version of Chrome I have :
and then what version of Chrome Driver I download
So you can see that I have a version of Chrome with quite a few minor patch releases since 89.0.4389.23, my minor version number is 128,
but again, it just matters that the major version numbers are the same, you can see when looking at all the available Chrome Driver downloads that this is the closest to my exact version and is the best choice
Also at the very top of the page it is guiding you, version 89 of Chrome, use this driver,
Download the correct driver
2. Make a folder somewhere out of your way to put this driver executable in, I like to put mine in Program Files, you are about to add this folder to the System Path so that Selenium can find it when you run your script.
Copy chromedriver.exe into that folder
3. Search for “Environment” in your Windows search bar
Select Path from the System Variables in the bottom selection field and click the “Edit” button
Click the “New” button and add the path to the folder that you just made that has chromedriver.exe in it
4. Create a file called login.py and put in this code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Optionsoptions = Options()
options.add_argument("--user-data-dir=C:\\Users\\Ashley\\Desktop\\UserData")options.page_load_strategy = 'normal'driver = webdriver.Chrome(options=options)driver.get("https://www.pexels.com/")
You can see in the code snippet where I put the path to my UserData folder.
Don’t confuse this with the folder to chromedriver.exe. I’m talking about the empty folder UserData you made at the beginning. Encode the path to that in your python script.
You MUST escape your Windows style slashes in python when hard-coding a path.
Use your own path to the UserData folder that you just made.
5. Run the python file using
6. It will launch the Chrome browser to the page you passed to driver.get
You can use whatever website you want, I just chose pexels.com as an example,
7. Manually login to the website. I’m saying manually as opposed to the word programmatically. What I mean is just login to the website like you would normally login to the website as a user. This will create the cookie. Because of the user-data-dir variable you passed to chromedriver in your script its going to save that cookie into the folder you specified.
8. Close the browser
The next time you launch your script it will load your cookies and you will already be logged in to your site.