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

To build a basic web bot with Selenium and Python, install Selenium, start a browser session, navigate to a page you are permitted to automate, locate an element, interact with it, wait for the expected result, and close the session with driver.quit(). Selenium controls a real browser through WebDriver; it does not grant permission to access or automate a website.

What a Selenium web bot does

Selenium’s Python bindings send WebDriver commands to a browser such as Chrome. That makes Selenium useful for testing web applications and for automating permitted repetitive interactions that depend on browser-rendered pages. A typical script follows this lifecycle: create a driver, open a page, find an element, act on it, verify the result, and end the browser session.

Choose a target and task you are authorized to automate. A site’s access rules and terms determine what is permitted; Selenium itself does not resolve that question. The example below uses a public test page. Replace its URL and locators with values verified on your authorized target.

Install Selenium and prepare Python

The Selenium Python API documentation currently lists Python 3.10 or newer. Check the current API documentation for supported versions and browser details when setting up, since compatibility can change.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

Create a project environment

From a terminal, create a folder for the script and a virtual environment. Activate it using the command for your operating system:

  • macOS or Linux: python3 -m venv .venv, then source .venv/bin/activate.
  • Windows PowerShell: py -m venv .venv, then .venvScriptsActivate.ps1.

Install Selenium in the activated environment:

python -m pip install -U selenium

Using a virtual environment keeps the project’s packages separate from other Python projects. Selenium’s current installation guidance recommends considering one.

Browser and driver setup

For most supported local browser setups, Selenium Manager handles obtaining the needed driver when the script creates a WebDriver instance. You generally do not need to download a driver manually first. Manual browser or driver configuration remains possible when your environment requires it. The Selenium API documentation describes Chrome, Edge, Firefox, Safari, WebKitGTK, and WPEWebKit among its supported options; support depends on platform and current versions.

A basic local script does not require a Selenium server or Grid. Remote WebDriver and Grid are options for running browsers on another machine or distributing sessions, not prerequisites for this beginner workflow.

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

Build and run a complete browser bot

Save the following as bot.py. It opens Selenium’s example page, checks the page title, waits for the heading to become visible, prints the heading, and always quits the browser session—even if a step raises an error.

Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

URL = "https://www.selenium.dev/selenium/web/web-form.html"


def main():
    driver = webdriver.Chrome()
    try:
        driver.get(URL)

        if "Web form" not in driver.title:
            raise RuntimeError(f"Unexpected page title: {driver.title!r}")

        wait = WebDriverWait(driver, 10)
        heading = wait.until(
            EC.visibility_of_element_located((By.TAG_NAME, "h1"))
        )
        print("Page heading:", heading.text)

        text_box = wait.until(
            EC.visibility_of_element_located((By.NAME, "my-text"))
        )
        text_box.send_keys("Selenium and Python")

        submit_button = wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, "button"))
        )
        submit_button.click()

        message = wait.until(
            EC.visibility_of_element_located((By.ID, "message"))
        )
        expected = "Received!"
        if message.text != expected:
            raise AssertionError(
                f"Expected {expected!r}, received {message.text!r}"
            )
        print("Form result:", message.text)

    finally:
        driver.quit()


if __name__ == "__main__":
    main()

Run it from the activated environment with python bot.py. The browser opens, fills and submits the example form, and prints the result. The interaction pattern follows Selenium’s official first-script guide.

What to change for your own target

  • Set URL to the page you may automate.
  • Inspect the page and replace By.NAME, By.CSS_SELECTOR, and By.ID values with locators matching its actual markup.
  • Replace the expected title and result text with outcomes that prove the task completed.
  • Keep the try/finally cleanup around browser work so failures do not leave a session running.

Find elements with stable locators

A locator identifies the page element Selenium should operate on. Prefer a stable ID or name when the page provides one. Otherwise, use a CSS selector anchored to a stable attribute, such as a form field’s name or an element’s dedicated test attribute. These are easier to understand and maintain than selectors based on incidental layout.

Locator Example When to use it
ID (By.ID, "message") When the element has a stable, unique ID.
Name (By.NAME, "my-text") When a form control has a stable name.
CSS selector (By.CSS_SELECTOR, "button[type='submit']") When a stable attribute or relationship identifies the element.
Tag name (By.TAG_NAME, "h1") When the page structure guarantees the relevant element is unambiguous.

Avoid generated class names that change between builds and long absolute XPath expressions tied to a particular page layout when a stable attribute is available. If a locator finds multiple elements or stops working after a site update, verify the current markup and narrow the selector to a stable, unique target.

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.

Wait for the condition your next action needs

A navigation reaching its configured document readiness state does not guarantee that JavaScript-driven content or later page updates are ready. Selenium identifies timing races as a common source of flaky automation. Its waiting strategies explain why page-load completion alone may not mean an element is ready.

Use explicit waits for meaningful states

WebDriverWait(driver, 10).until(...) checks a condition until it succeeds or times out. Select a condition that matches the next operation:

Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
  • EC.visibility_of_element_located(locator) when the element must be visible before reading or typing.
  • EC.element_to_be_clickable(locator) before clicking a control.
  • Other expected conditions when the next step depends on a specific state, such as an element becoming present or a URL changing.

The example waits for visibility before reading or typing and for clickability before clicking. A fixed delay such as time.sleep(5) always waits the full duration regardless of whether the page is ready, and may still be too short. Selenium project guidance recommends condition-based waits and advises against sleeps. Do not mix implicit and explicit waits in the same session; the combined timing can be difficult to reason about.

Handle errors and end the session reliably

The finally block calls driver.quit() whether the script succeeds or fails. This ends the WebDriver session and closes its associated browser windows. driver.close() closes only the current window or tab, so it is not the general cleanup call when the whole session should end.

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

The example lets useful errors surface: a failed title check raises a runtime error, a failed assertion reports an unexpected result, and a wait that expires raises a timeout error. When adding custom error handling, report the URL, action, and locator involved; do not silently ignore failures that could make the bot appear successful when it was not.

When to use remote WebDriver or Grid

Start with a local browser for a single script. Consider remote WebDriver when the browser needs to run on a different host, or Selenium Grid when a testing setup needs remote or distributed sessions. The Selenium API documentation links to its Remote WebDriver guidance. Remote execution adds environment and connectivity considerations, so it is usually unnecessary for a first local bot.

Or skip the browser setup

If your task is to capture a page as an image or PDF rather than interact with its controls, ScreenshotNeo provides a website screenshot API. A single GET request returns a PNG, JPEG, WebP, or PDF. It is not a substitute for Selenium when you need to fill a form or click through an interactive workflow.

Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft

For an image capture, replace the example URL with the page you need and set your API key:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo documentation for request options. ScreenshotNeo removes cookie and consent banners, newsletter popups, and chat widgets before capture; each cleanup step can be turned off. Bot checks and CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing, and response headers report the page verdict and billing status. Its MCP server offers screenshot tools for AI agents. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000.

Sign up for ScreenshotNeo’s free plan to try 1,000 screenshots a month with no card.

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

Troubleshooting common Selenium problems

Selenium cannot start the browser

Check that the browser you intend to use is installed and supported on your platform, that the virtual environment containing Selenium is active, and that the package installation completed. Selenium Manager handles most ordinary driver setup, but restricted networks, unusual browser installations, or environment-specific policies can require manual configuration. Review the error message and current setup guidance in the Selenium Python API documentation.

The element cannot be found

Confirm that navigation reached the expected page and inspect the current markup for the element’s actual ID, name, or stable attributes. The element may not yet exist because the page updates asynchronously; wait for the required condition rather than searching immediately. Check whether the content is inside a frame or another browsing context before assuming the locator is wrong.

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

The wait times out

A timeout means the condition did not become true within the configured limit. Verify the locator, the target page, and whether the expected state can occur at all. If the site is slow or the condition legitimately takes longer, adjust the timeout deliberately; increasing it will not fix an incorrect locator or a blocked navigation.

Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.

The click or form submission has no visible effect

Wait for the control to be clickable before clicking, then wait for a visible result or another clear completion condition. Check that the selector identifies the intended control and that an overlay or validation state is not preventing the action. Do not treat the absence of an exception as proof that the workflow succeeded; verify a page outcome.

The browser remains open after an error

Put all browser operations after driver creation inside a try block and call driver.quit() in finally, as in the example. A cleanup call placed only at the end of the success path will be skipped when an earlier operation fails.

Frequently asked questions

Does Selenium work without opening a browser window?

This example starts a normal browser session. Whether a browser can run without a visible window depends on the browser and environment configuration; the tutorial does not require that mode.

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

Is Selenium only for testing?

Selenium is commonly used to test web applications, and its browser-control workflow can also automate repetitive interactions when the target site and task permit it.

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.