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
- Step 1: Install the Latest Selenium
- Step 2: Launch a Browser with Selenium Manager
- Step 3: Create Drivers Using the New Options API
- 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
Make sure it works:
python -m selenium --version
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
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)
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()
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)
Final Notes
In the next article, we’ll cover page navigation, tabs, clicks, forms, scrolling, and running JavaScript.
Meanwhile, here are some useful resources:
- The Complete Guide to Web Scraping with Selenium in Python
- Join our Discord
- Selenium Scraping Examples in Python and NodeJS (GitHub) If you want any examples I might have missed, leave a comment and I’ll add them.
Top comments (0)