avatarCoşkun Deniz

Summary

This web content provides a comprehensive guide on setting up Python and Selenium for browser automation, detailing the installation of the Selenium library, WebDriver binaries, and best practices for driver management.

Abstract

The article titled "Browser Automation with Python and Selenium — 1: Installation" serves as an introductory guide for individuals looking to automate web browsers using Python and the Selenium framework. It begins by emphasizing the importance of using a virtual environment to manage project dependencies and then outlines the steps to install the Selenium library via pip or from a PyPI source archive. The author highlights the necessity of WebDriver binaries, which act as intermediaries between Selenium and the browsers being automated. The article presents two methods for managing these drivers: using the author's webdriver-setup package for ease of use, or manual download and path configuration. It concludes with a note on creating driver instances and a teaser for the next post, which will feature a practical example of webdriver usage.

Opinions

  • The author advocates for the use of virtual environments to prevent dependency conflicts.
  • The webdriver-setup package is recommended by the author for its user-friendly approach to managing WebDriver binaries.
  • The author provides a subjective opinion that managing WebDriver binaries manually is less convenient than using a package, yet still provides detailed instructions for those who prefer this method.
  • The article suggests that the webdriver-setup package, which the author has developed, offers an easy-to-use interface for downloading and creating webdriver instances.
  • The author implies that the manual management of drivers is more complex but includes comprehensive steps for users who opt for this approach, including setting up the system path and authorizing Safari's WebDriver on macOS.
  • The author's mention of the built-in Safari WebDriver for El Capitan and newer macOS versions indicates a preference for native solutions where available.
  • By providing references to official Selenium documentation,

Browser Automation with Python and Selenium — 1: Installation

Towards to starting…

Photo by Vanessa Bucceri on Unsplash

In the previous post, I made a quick introduction to the Selenium project. We will look at the installation steps to get started in this post.

We need to install the Python language binding to use Selenium with Python in our automation project. Currently supported Python versions are 3.5 and above. In addition to that, we will need WebDriver binaries for the browsers we want to automate and run the commands on.

Installing the Selenium library

  • First things first, always use a virtual environment for your projects to isolate dependencies from the rest of the system.
python -m venv env
source env/bin/activate
  • Install the selenium library using pip.
pip install selenium
python setup.py install

Installing WebDriver binaries

Drivers control the actual browsers. They are mostly created by browser vendors. As stated in the official documentation

Drivers are generally executable modules that run on the system with the browser itself, not on the system executing the test suite. (Although those may be the same system.)

You need to have browser-specific WebDriver binaries installed to be able to control the browser.

There are 2 options to manage drivers: either manually or by using a package for this purpose.

1. Managing driver binaries with a python package

Through these series of posts, I will mostly use the webdriver-setup package written by me on top of the webdriver-manager to provide easy to use interface for webdriver binary download and instance creation.

  • Install the package with pip.
pip install webdriver-setup
  • Use it in your script as follow to download the binary and create the webdriver instance.
from webdriver_setup import get_webdriver_for
driver = get_webdriver_for("firefox")

2. Managing driver binaries manually

There are 2 steps for manual management:

  1. Download the WebDriver binary supported by your preferred browser from the links below.

Note that the Safari webdriver is built-in for El Capitan and newer versions of macOS.

2. Place the binary in a directory that is already in your system path or manually pass the directory path where you put your binary with executable_path argument.

For example

  • Create a directory to place the executables.
Linux/Mac: /opt/WebDriver/bin
Windows: C:\WebDriver\bin
  • Add the directory to your PATH.

For Linux/Mac, run the following from a terminal, and source the .profile or reopen the terminal to activate.

export PATH=$PATH:/opt/WebDriver/bin >> ~/.profile

For Windows, open a command prompt as administrator and run the following command to permanently add the directory to your path for all users on your machine.

setx /m path "%path%;C:\WebDriver\bin\"

You can put the binary under one of the paths currently in the system path like /usr/bin, /usr/local/bin for Linux/Mac.

As an example, if you run chromedriver from your terminal, you will get an output like below.

Starting ChromeDriver 86.0.4240.198 (d8a506935fc2273cfbac5e5b629d74917d9119c7-refs/branch-heads/4240@{#1431}) on port 9515
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.

But we won’t run it manually.

Creating driver instances

Creating an instance is common for all browsers. Replace <browser> in the following template with the browser interface you want to use like Firefox, Chrome, Opera, Safari, Edge, or Ie.

If you are using the webdriver-setup package, you can give the browser name as an argument to download its webdriver binary and create the instance.

from webdriver_setup import get_webdriver_for
driver = get_webdriver_for("chrome")

For Safari on macOS High Sierra and later, run the following command from the terminal for the first time to authorize WebDriver.

safaridriver --enable

Things to Remember

  • You need to download the specific webdriver binary for your browser of choice and update the system path if you put your binary in a directory that is not in your path by default. Alternatively, you can use a package to manage the download and creation of the webdriver instance in your code.
  • You need to install the selenium package.

In the next post, I will show and explain a simple example of webdriver usage.

References

  1. https://www.selenium.dev/documentation/en/selenium_installation/
  2. https://www.selenium.dev/documentation/en/webdriver/understanding_the_components/
  3. https://www.selenium.dev/documentation/en/webdriver/driver_requirements/#quick-reference

Thank you for your time.

Selenium
Python
Browser Automation
Selenium Webdriver
Technology
Recommended from ReadMedium