How to Install Python and Selenium for Browser Automation

2020-10-18 16:15:20 | #programming #python #automation | Part 1 of 3

Tested On

  • Linux Ubuntu 20.04
  • Windows 10
  • macOS Catalina

Browser automation is one of the most useful applications of Python programming. Just like you learned how to interface with a browser with your mouse and keyboard, programs/bots can too. This includes button clicks, form submissions, scrolling, search, navigation, etc. And you won't need to learn any complex computer science to get this up and running—we provide plenty of sample code you can modify for your specific use case.

This guide will serve as an introductory tutorial, with the goal being to set up your environment and install the necessary software to automate four of the major browsers. You'll also learn how to write a simple Python script, to verify that everything is working, as intended. Future tutorials will dive deep into what's possible with Python and Selenium. We also provide an Automation the Easy Way with Python course, for those looking to get a more complete understanding of automation.

How to Set Up a Project Skeleton

How to Create Python Project Files with Windows 10 PowerShell 2.0+

cd ~
New-Item -ItemType "directory" -Path ".\python-browser-automation"
cd python-browser-automation
New-Item -ItemType "file" -Path . -Name "main.py"
New-Item -ItemType "directory" -Path ".\drivers"
virtualenv venv
.\venv\Scripts\activate

To verify that the virtual environment is active, make sure (venv) is in the PowerShell command prompt. For example, (venv) PS C:\Users\username\python-browser-automation>

How to Create Python Project Files with Linux Ubuntu 14.04+ or macOS

cd ~
mkdir python-browser-automation
cd python-browser-automation
virtualenv -p python3 venv
source venv/bin/activate
mkdir drivers
touch main.py

To verify that the virtual environment is active, make sure (venv) is in the terminal command prompt.

This will create the following files and folders, and activate the virtual environment.

▾ python-browser-automation/
  ▸ venv/
  ▸ drivers/
  main.py

How to Install Selenium with Pip

To install Selenium, switch over to your terminal window, activate your virtual environment with source venv/bin/activate on Ubuntu/macOS, or .\venv\Scripts\activate on Windows 10, and run pip3 install selenium. You then, want to update your dependency manifest, by running pip3 freeze > requirements.txt.

How to Install a Browser Driver and Run an Automation Script with Python and Selenium

To emulate browser behavior, we'll need an instance of that browser's driver. A browser driver is software that implements Selenium's Web Driver protocol, acting as a bridge, in order to convert Selenium commands into browser-friendly API commands. This allows us to automate browser actions we would normally perform with our mouch, touch, or keyboard. Each browser has its own proprietary API, but Selenium commands make it easy to learn one set standard/language that is compatible with everything.

How to Automate the Chrome Browser

How to Install the Chrome Driver

Chrome drivers can be found on the Chrome driver downloads page. For this tutorial, we're going to use version 86.0.4240.75 (Official Build) (64-bit), but you're required to install the driver that matches your existing Chrome version. Click the three dots in the top right corner, then Help > "About Google Chrome". Take a note your specific Chrome version. Then, download the driver compatible with your operating system and save the chromedriver file to your drivers folder.

To make things simple, install the latest version of Chrome and the equivalent Chrome driver. If you'd like to upgrade Chrome to the latest release on Linux Ubuntu, from the command line, run sudo apt-get --only-upgrade install google-chrome-stable. If you're on other operation systems, head over to the Chrome downloads page.

How to Write a Chrome Browser Automation Script with Python and Selenium

Add the following code to your main.py file:

from selenium import webdriver

driver = webdriver.Chrome(executable_path='drivers/chromedriver')

# Navigate to page
driver.get('https://google.com')
print(driver.title)

driver.quit()

Run this program with python main.py. If everything was set up properly, you should see a Chrome browser window open, close, and then "Google" printed to the console.

How to Automate the FireFox Browser

How to Install the Firefox Driver

Firefox drivers can be found on the Geckodriver releases page. For this tutorial, we're going to use version 81.0.2 (64-bit). We recommend that you install the latest version of Firefox. To see which version you're using, click the hamburger menu in the top right corner, then Help > "About Firefox". Download the driver compatible with your operating system and save the geckodriver file to your drivers folder.

If you'd like to upgrade Firefox to the latest release on Linux Ubuntu, from the command line, run sudo apt-get --only-upgrade install firefox. If you're on other operation systems, head over to the Firefox downloads page.

How to Write a Firefox Browser Automation Script with Python and Selenium

Add the following code to your main.py file:

from selenium import webdriver

driver = webdriver.Firefox(executable_path='drivers/geckodriver')

# Navigate to page
driver.get('https://google.com')
print(driver.title)

driver.quit()

Run this program with python main.py. If everything was set up properly, you should see a Firefox browser window open, close, and then "Google" printed to the console.

How to Automate the Microsoft Edge Browser

How to Install the Microsoft Edge WebDriver

Microsoft Edge drivers can be found on the Edge WebDriver download page. For this tutorial, we're going to use version 88.0.673.0, but you're required to install the driver that matches your existing Edge version. Click "Settings and more" at the top of the window, then scroll down and select > "About Microsoft Edge". Take a note your specific Edge version. Then, download the driver compatible with your operating system and save the msedgedriver.exe file to your drivers folder.

To make things simple, install the latest version of Edge and the equivalent Edge driver.

How to Write a Microsoft Edge Browser Automation Script with Python and Selenium

Add the following code to your main.py file:

from selenium import webdriver

driver = webdriver.Edge(executable_path='drivers/msedgedriver.exe')

# Navigate to page
driver.get('https://google.com')
print(driver.title)

driver.quit()

Run this program with python main.py. If everything was set up properly, you should see an Edge browser window open, close, and then "Google" printed to the console.

How to Automate the Safari Browser

How to Install the Safari WebDriver

With the Safari browser installed, you should already have the Safari browser on your machine. This file is normally found at /usr/bin/safaridriver. To enable WebDriver support, you must configure Safari with the safaridriver --enable command, once, on High Sierra and later. On Sierra and earlier, carry out the following steps:

  1. Click Safari > Preferences, and "Show Develop menu in menu bar" under the Advaced tab
  2. Click Develop > Allow Remote Automation
  3. Manually run /usr/bin/safaridriver and follow the authentication prompts

How to Write a Safari Browser Automation Script with Python and Selenium

Add the following code to your main.py file:

from selenium import webdriver

driver = webdriver.Safari(executable_path='/usr/bin/safaridriver')

# Navigate to page
driver.get('https://google.com')
print(driver.title)

driver.quit()

Run this program with python main.py. If everything was set up properly, you should see a Safari browser window open, close, and then "Google" printed to the console.

Conclusion

You should have everything you need, at this point, to start automating your browser. Subscribe to get notified of future tutorials, where you'll learn how to automate specific operations, such as clicks, keyboard input, form submissions, and so on.

If you're interested in programs that carry out your computer tasks for you, take our Automation the Easy Way with Python course. This course teaches CSV and Excel file generation, API requests, website scraping, email delivery, task scheduling, and browser click, mouse, and keyboard automation. Automate your daily tasks, free up time, and get ahead, today.

Want To See More Exercises?

View Exercises View Courses

Comments

You must log in to comment. Don't have an account? Sign up for free.

Subscribe to comments for this post

Want To Receive More Free Content?

Would you like to receive free resources, tailored to help you reach your IT goals? Get started now, by leaving your email address below. We promise not to spam. You can also sign up for a free account and follow us on and engage with the community. You may opt out at any time.



Tell Us About Your Project









Contact Us

Do you have a specific IT problem that needs solving or just have a general IT question? Use the contact form to get in touch with us and an IT professional will be with you, momentarily.

Hire Us

We offer web development, enterprise software development, QA & testing, google analytics, domains and hosting, databases, security, IT consulting, and other IT-related services.

Free IT Tutorials

Head over to our tutorials section to learn all about working with various IT solutions.

Contact