Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

Selenium lets Python control a real browser, wait for JavaScript-rendered content, and read the elements that appear on the page. To get started, install the Selenium package, open a browser session, navigate to a page, locate the content you need, wait for it to be ready, and close the session when you finish. Selenium is a browser automation tool, not permission to collect data: check the target website’s rules first and stop if access is denied or automation is disallowed.

What Selenium does—and when to use it

Selenium WebDriver is a language-neutral API and protocol for controlling browser behavior. A WebDriver session sends commands to a browser through a driver implementation, letting your code navigate pages, find elements, click controls, enter text, and read rendered content. Selenium supports major browsers and can run locally or through Selenium Server for remote execution. Selenium’s getting-started guide explains the basic components; its WebDriver overview describes the broader system.

A browser can be useful when the information you need appears only after JavaScript runs, or when reaching it requires ordinary page interactions. If a page already serves the needed data in a simpler accessible format, a browser session may be more machinery than the task requires. Selenium does not guarantee access to a site, defeat its protections, or make scraping permissible.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Install Selenium and prepare a browser

The Python client documentation retrieved on September 29, 2026, is labeled Selenium 4.49.0 and lists Python 3.10 or later. Check the current Python client documentation for requirements that may change after that version. Use an isolated environment so the project’s dependencies do not interfere with other Python work.

  1. Install Python 3.10 or later if it is not already available.

  2. Create and activate a virtual environment in your project directory. For macOS or Linux, run python3 -m venv .venv, then source .venv/bin/activate. For Windows PowerShell, run py -m venv .venv, then .venvScriptsActivate.ps1.

  3. Install or upgrade the Selenium binding with python -m pip install -U selenium.

    Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  4. Install a browser supported by Selenium, such as Chrome, Edge, Firefox, or Safari, as appropriate for your operating system. Browser availability varies by platform.

What Selenium Manager handles

For an ordinary local setup, current Selenium bindings can use Selenium Manager when you have not supplied a driver. Selenium Manager discovers browser and driver versions, downloads driver artifacts, and caches them; its documentation says automated browser management has been available since Selenium 4.11.0. This avoids a common manual download-and-path setup, but unusual environments—such as those using a proxy or controlled driver versions—may need additional configuration. See the Selenium Manager documentation.

When to manage a driver yourself

Let Selenium Manager choose the driver when learning or running a typical local script. A manually managed driver path can be useful when an environment requires explicit version or path control. That trade-off is convenience versus direct control; it is not a general performance ranking.

Run a first scraping script

This example reads the page title and headings from a public example page. Replace the URL and selectors with ones verified against a site you are permitted to access. An element’s presence in the page’s DOM matters: a selector copied from another page will not automatically match your target.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Save the code as scrape.py, activate the virtual environment, then run python scrape.py.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

URL = "https://www.selenium.dev/"

# Selenium Manager supplies a driver for an ordinary local setup.
driver = webdriver.Chrome()

try:
    driver.get(URL)

    # Wait for the document title to be non-empty before extracting it.
    WebDriverWait(driver, 10).until(
        lambda browser: browser.title.strip() != ""
    )

    print("Title:", driver.title)

    # Find all h2 elements that are present in the DOM at this point.
    headings = driver.find_elements(By.CSS_SELECTOR, "h2")
    for heading in headings:
        text = heading.text.strip()
        if text:
            print(text)
finally:
    # Close the browser and end the WebDriver session even if extraction fails.
    driver.quit()

The script follows the core lifecycle: create a session, navigate with get, wait for a needed condition, locate and read elements, and call quit. Selenium’s first-script tutorial demonstrates a comparable session flow with a form. Here, the selectors are only examples; inspect your target page and confirm its actual structure before relying on them.

Read the fields you actually need

For each element, .text returns its visible text. To read an HTML attribute—such as a link destination—use element.get_attribute("href"). For example, after locating links with driver.find_elements(By.CSS_SELECTOR, "a"), loop through them and read their text and href values, ignoring missing or irrelevant links. Extracting only the fields required for your task makes the result easier to check and store.

The example prints results to the terminal; it does not save them to a file or database. Choose a storage format that fits your task, validate the extracted values, and handle missing fields rather than assuming every record has the same shape.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose locators and collect one or many elements

A locator tells Selenium how to find an element in the DOM. The available approaches include ID, name, CSS selector, class name, link text, partial link text, tag name, and XPath. Selenium’s locator guide describes the strategies, and its locator tips recommend choosing locators that are clear and resilient to page changes.

Use the modern By form shown in the example, such as By.ID or By.CSS_SELECTOR. A singular call like find_element returns the first matching element in the current search context; it does not confirm that the match is the one you intended. Use find_elements to get all matches—an empty list if none match—and iterate when collecting repeated records. Selenium documents this behavior in its finder guide.

Wait for dynamic pages by condition

A completed browser navigation does not mean a JavaScript application has finished rendering the content you want. If your script runs ahead of the page, it can fail intermittently: sometimes the page reaches the needed state first, and sometimes the script does. Selenium calls these timing issues race conditions and recommends explicit waits for the condition that matters. See Waiting Strategies.

For example, import WebDriverWait and expected_conditions, then wait for a specific element to appear:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

result = WebDriverWait(driver, 15).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, "article .result"))
)
print(result.text)

Replace article .result with a selector from the actual target page. Use the condition that fits the next action: presence in the DOM if you need to read an element, visibility if it must be displayed, or clickability if you are about to click it. An element can exist in the DOM without being visible or ready for interaction.

Explicit waits versus implicit waits and fixed sleeps

An explicit wait is targeted: it polls for a particular condition and raises a timeout error if the condition does not become true within the limit. An implicit wait sets a broader timeout for element-finding operations rather than describing the specific state needed at a particular step. Selenium’s first-script tutorial presents an implicit wait as an easy placeholder and says it is rarely the best solution. Avoid mixing implicit and explicit waits without understanding their combined timing behavior.

A fixed sleep pauses for a predetermined duration whether the page is ready or not. It can waste time when the page is fast and still fail when it is slow. Prefer a condition-based wait; use a fixed delay only when a known timing requirement cannot be expressed as a condition.

Interact with pages carefully

Once the needed element is ready, Selenium can use the same browser controls a visitor would use: enter text with send_keys, activate a control with click, and read the resulting text or attributes. Verify that each action is appropriate for the site and permitted by its rules. A click that changes the page may require a new wait for the next state before you read more content.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For longer-running collection, keep the browser session lifecycle explicit: recover from expected missing or changing elements, avoid uncontrolled request volume, and ensure driver.quit() runs even on failure. The try/finally pattern in the example closes the browser session if extraction raises an exception.

Check site rules and handle access limits

Selenium’s documentation cautions that some websites do not permit scraping and others may block Selenium. Its use-cases guidance tells users to be familiar with a website’s terms of service. Check the specific site’s current terms and any applicable access requirements before collecting data; this guide cannot determine whether a particular use is allowed.

Local versus remote browser execution

A local browser is the simplest route for learning and small tasks: the Python script and browser run on your machine. Selenium also supports remote execution through Selenium Server, which is useful when you deliberately configure a remote browser or grid. Remote execution adds infrastructure and configuration, so start locally unless your environment or workload calls for a remote session. Selenium’s getting-started documentation covers the setup concepts; deployment choice is a practical decision, not a performance guarantee.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshooting common problems

Driver or browser startup fails

Confirm that a supported browser is installed and that the Selenium package is installed in the active Python environment. Selenium Manager handles typical driver discovery when no driver is supplied, but proxies, restricted networks, or a need for a pinned driver can require environment-specific setup. Check the Selenium Manager documentation before switching to a manual driver path.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

NoSuchElementException or an empty result list

The selector may not match the current page, the search context may be wrong, or the element may not have been inserted yet. Inspect the page’s DOM, verify the selector in the browser’s developer tools, and wait for the relevant condition before searching. If the content is inside a frame, locate and switch to the appropriate frame before looking for its elements.

The script reads blank or incomplete text

The element may exist before the application fills it in, may not be visible, or may contain text somewhere else in the page structure. Wait for the content or visibility condition that matches your goal, then inspect the element’s text and relevant attributes. Confirm that you are reading the correct element rather than the first match of a broad selector.

Timeout while waiting

A timeout means the condition did not become true within the chosen limit. Check whether the selector and condition are correct, whether the page reached the expected state, and whether the site changed its structure. Increase the timeout only when a legitimate slow load explains the delay; a longer timeout will not repair an incorrect selector or denied access.

The site blocks automation or refuses access

Blocking is not a driver error to work around. Recheck the site’s rules and stop if automated collection is not allowed or access is denied. Selenium’s own documentation explicitly warns that sites may block it.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The browser stays open after an error

Put browser work inside try/finally and call driver.quit() in the finally block. This ends the session whether extraction succeeds or raises an exception.

Or skip the browser setup

If your goal is a screenshot or PDF rather than extracting structured text, ScreenshotNeo is a website screenshot API and MCP server for developers. A single GET request can return a PNG, JPEG, WebP, or PDF. The call below follows the API’s documented pattern; see the ScreenshotNeo API documentation for parameters and response details.

curl -G "https://api.screenshotneo.com/v1/shot" 
  -d access_key=YOUR_API_KEY 
  --data-urlencode url=https://stripe.com 
  -o shot.webp

ScreenshotNeo accepts cookie or consent banners before capture and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each step can be turned off. Bot checks and CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers identify the page verdict and billing status. Its MCP server offers take_screenshot, get_page_info, and capture_pdf for Claude, Cursor, and other MCP clients. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 shots. See ScreenshotNeo for the service and plans. Sign up for the free plan.

Frequently Asked Questions

Does Selenium scrape a page’s source HTML or rendered content?

WebDriver controls a browser and can inspect its DOM after the page has rendered; it can also interact with page elements. The example reads rendered element text rather than downloading and parsing a raw response.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Can I use Selenium without installing a browser driver manually?

For ordinary local setups, current Selenium bindings can invoke Selenium Manager to discover and obtain a driver when you have not supplied one. Special network or version-control requirements may need configuration.

Can Selenium collect data from any website?

No. A site may prohibit scraping in its terms or block Selenium. Check the site’s rules and stop if access is denied or automation is disallowed.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.