DEV Community

Valentina Skakun for HasData

Posted on

Set Up Selenium for Python

This is the first post in series about web scraping with Selenium in Python. Here, you’ll learn how to install Selenium, set up a WebDriver, and launch your first browser.

Table of Contents

  1. Step 1: Install the Latest Selenium
  2. Step 2: Launch a Browser with Selenium Manager
  3. Step 3: Create Drivers Using the New Options API
  4. Step 4: Use Context Managers for Auto-quit

Step 1: Install the Latest Selenium

First, install Selenium from PyPI. It works with all modern browsers.

pip install selenium
Enter fullscreen mode Exit fullscreen mode

Make sure it works:

python -m selenium --version
Enter fullscreen mode Exit fullscreen mode

If you see the version number, you’re good to go.

Step 2: Launch a Browser with Selenium Manager

Now let’s start a browser with Selenium.
You don’t need to download ChromeDriver or GeckoDriver anymore, Selenium handles it automatically.

from selenium import webdriver

# Chrome
driver = webdriver.Chrome()

# Firefox
driver = webdriver.Firefox()

# Edge
driver = webdriver.Edge()

# Safari (Mac only)
driver = webdriver.Safari()

driver.quit()  # Always close your browser when done
Enter fullscreen mode Exit fullscreen mode

If you run this and see the browser open and close, it works.
To use your own driver binary:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

service = Service("/path/to/chromedriver")
driver = webdriver.Chrome(service=service)
Enter fullscreen mode Exit fullscreen mode

Step 3: Create Drivers Using the Options

When you launch a browser, you can pass options, small settings that control the browser.
For example, run it in headless mode (without opening a real window) or change the user agent.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("--headless")  # Run without a visible window
options.add_argument("--window-size=1920,1080")  # Set window size
options.add_argument("--user-agent=MyScraperBot/1.0")  # Custom user agent

driver = webdriver.Chrome(options=options)
driver.get("https://example.com")

print(driver.title)  # Just to check everything works

driver.quit()
Enter fullscreen mode Exit fullscreen mode

Headless mode is better for scraping, as it’s faster and uses less memory.

Step 4: Use Context Managers for Auto-quit

When you open a browser in Selenium, it stays running until you call driver.quit(). If your script crashes or you forget to close it, the browser keeps running in the background.
To avoid that, use a context manager (with block). It closes the browser automatically when you’re done, even if an error happens.

from selenium import webdriver

# Browser will close automatically after the block ends
with webdriver.Chrome() as driver:
    driver.get("https://example.com")
    print(driver.title)
Enter fullscreen mode Exit fullscreen mode

Final Notes

In the next article, we’ll cover page navigation, tabs, clicks, forms, scrolling, and running JavaScript.
Meanwhile, here are some useful resources:

Top comments (0)