selenium

Official Python bindings for Selenium WebDriver

Apache-2.0 230 个版本 Python >=3.10
安装
pip install selenium
poetry add selenium
pipenv install selenium
conda install selenium
描述

====================== Selenium Client Driver

Introduction

Python language bindings for Selenium WebDriver.

The selenium package is used to automate web browser interaction from Python.

+-------------------+--------------------------------------------------------+ | Home: | https://selenium.dev | +-------------------+--------------------------------------------------------+ | GitHub: | https://github.com/SeleniumHQ/Selenium | +-------------------+--------------------------------------------------------+ | PyPI: | https://pypi.org/project/selenium | +-------------------+--------------------------------------------------------+ | IRC/Slack: | https://selenium.dev/support/#ChatRoom | +-------------------+--------------------------------------------------------+ | Docs: | https://selenium.dev/selenium/docs/api/py | +-------------------+--------------------------------------------------------+ | API Reference:| https://selenium.dev/selenium/docs/api/py/api.html | +-------------------+--------------------------------------------------------+

Updated documentation published with each commit is available at: readthedocs.io <https://selenium-python-api-docs.readthedocs.io/en/latest>_


Supported Python Versions

  • Python 3.10+

Supported Browsers

Several browsers are supported, as well as the Remote protocol:

  • Chrome
  • Edge
  • Firefox
  • Safari
  • WebKitGTK
  • WPEWebKit

Installing

Install or upgrade the Python bindings with pip <https://pip.pypa.io/>.

Latest official release::

pip install -U selenium

Nightly development release::

pip install -U --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ selenium

Note: you should consider using a virtual environment <https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments>_ to create an isolated Python environment for installation.

Drivers

Selenium requires a driver to interface with the chosen browser (chromedriver, edgedriver, geckodriver, etc).

In older versions of Selenium, it was necessary to install and manage these drivers yourself. You had to make sure the driver executable was available on your system PATH, or specified explicitly in code. Modern versions of Selenium handle browser and driver installation for you with Selenium Manager <https://selenium.dev/documentation/selenium_manager>_. You generally don't have to worry about driver installation or configuration now that it's done for you when you instantiate a WebDriver. Selenium Manager works with most supported platforms and browsers. If it doesn't meet your needs, you can still install and specify browsers and drivers yourself.

Links to some of the more popular browser drivers:

+--------------+-----------------------------------------------------------------------+ | Chrome: | https://developer.chrome.com/docs/chromedriver | +--------------+-----------------------------------------------------------------------+ | Edge: | https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver | +--------------+-----------------------------------------------------------------------+ | Firefox: | https://github.com/mozilla/geckodriver | +--------------+-----------------------------------------------------------------------+ | Safari: | https://webkit.org/blog/6900/webdriver-support-in-safari-10 | +--------------+-----------------------------------------------------------------------+

Example 0:

  • launch a new Chrome browser
  • load a web page
  • close the browser

.. code-block:: python

from selenium import webdriver


driver = webdriver.Chrome()
driver.get("https://selenium.dev")
driver.quit()

Example 1:

  • launch a new Chrome browser
  • load the Selenium documentation page
  • find the "WebDriver" link
  • click the "WebDriver" link
  • close the browser

.. code-block:: python

from selenium import webdriver
from selenium.webdriver.common.by import By


driver = webdriver.Chrome()

driver.get("https://selenium.dev/documentation")
assert "Selenium" in driver.title

elem = driver.find_element(By.ID, "m-documentationwebdriver")
elem.click()
assert "WebDriver" in driver.title

driver.quit()

Example 2:

Selenium WebDriver is often used as a basis for testing web applications. Here is an example using Python's standard unittest <https://docs.python.org/3/library/unittest.html>_ framework:

.. code-block:: python

import unittest
from selenium import webdriver


class SeleniumTestCase(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Chrome()
        self.addCleanup(self.driver.quit)

    def test_page_title(self):
        self.driver.get("https://selenium.dev")
        self.assertIn("Selenium", self.driver.title)

Example 3:

Here is an example using pytest <https://pytest.org>_ framework:

.. code-block:: python

import pytest
from selenium import webdriver


@pytest.fixture
def driver():
    driver = webdriver.Chrome()
    yield driver
    driver.quit()


def test_page_title(driver):
    driver.get("https://selenium.dev")
    assert "Selenium" in driver.title

Selenium Grid (optional)

For local Selenium scripts, the Java server is not needed.

To use Selenium remotely, you need to also run a Selenium Grid. For information on running Selenium Grid: https://selenium.dev/documentation/grid/getting_started/

To use Remote WebDriver see: https://selenium.dev/documentation/webdriver/drivers/remote_webdriver/?tab=python

Use The Source Luke!

View source code online:

+---------------+-------------------------------------------------------+ | Official: | https://github.com/SeleniumHQ/selenium/tree/trunk/py | +---------------+-------------------------------------------------------+

版本列表
4.45.0 2026-06-16
4.44.0 2026-05-12
4.43.0 2026-04-10
4.42.0 2026-04-09
4.41.0 2026-02-20
4.40.0 2026-01-18
4.39.0 2025-12-06
4.38.0 2025-10-25
4.37.0 2025-10-17
4.36.0 2025-10-02
4.35.0 2025-08-12
4.34.2 2025-07-08
4.34.1 2025-07-07
4.34.0 2025-06-29
4.33.0 2025-05-23
4.32.0 2025-05-02
4.31.0 2025-04-05
4.30.0 2025-03-21
4.29.0 2025-02-20
4.28.1 2025-01-23
4.28.0 2025-01-20
4.27.1 2024-11-26
4.27.0 2024-11-25
4.26.1 2024-10-31
4.26.0 2024-10-30
4.25.0 2024-09-20
4.24.0 2024-08-28
4.23.1 2024-07-24
4.23.0 2024-07-19
4.22.0 2024-06-20
4.21.0 2024-05-16
4.20.0 2024-04-24
4.19.0 2024-03-27
4.18.1 2024-02-20
4.18.0 2024-02-19
4.17.2 2024-01-23
4.17.1 2024-01-23
4.17.0 2024-01-23
4.16.0 2023-12-06
4.15.2 2023-11-03
4.15.1 2023-11-02
4.15.0 2023-11-01
4.14.0 2023-10-10
4.13.0 2023-09-25
4.12.0 2023-08-31
4.11.2 2023-08-01
4.11.1 2023-08-01
4.11.0 2023-07-31
4.10.0 2023-06-07
4.9.1 2023-05-08
4.9.0 2023-04-21
4.8.3 2023-03-24
4.8.2 2023-02-17
4.8.1 2023-02-17
4.8.0 2023-01-23
4.7.2 2022-12-02
4.7.1 2022-12-02
4.7.0 2022-12-01
4.6.1 2022-11-25
4.6.0 2022-11-04
4.5.0 2022-09-28
4.4.3 2022-08-18
4.4.2 2022-08-17
4.4.1 2022-08-17
4.4.0 2022-08-09
4.3.0 2022-06-23
4.2.0 2022-05-27
4.1.5 2022-05-05
4.1.4 2022-05-05
4.1.3 2022-03-09
4.1.2 2022-02-24
4.1.1 2022-02-23
4.1.0 2021-11-22
4.0.0 2021-10-13
4.0.0.rc1 2021-09-02
4.0.0.b4 2021-06-07
4.0.0.b3 2021-04-14
4.0.0.b2 2021-03-16
4.0.0.b2.post1 2021-03-17
4.0.0.b1 2021-02-15
4.0.0.a7 2020-11-10
4.0.0rc3 2021-10-08
4.0.0rc2 2021-09-30
4.0.0a6 2020-05-28
4.0.0a6.post2 2020-06-05
4.0.0a6.post1 2020-05-28
4.0.0a5 2020-03-18
4.0.0a3 2019-10-01
4.0.0a1 2019-04-18
3.141.0 2018-11-01
3.14.1 2018-09-20
3.14.0 2018-08-02
3.13.0 2018-06-25
3.12.0 2018-05-08
3.11.0 2018-03-12
3.10.0 2018-03-05
3.9.0 2018-02-05
3.8.1 2018-01-04
3.8.0 2017-11-30
3.7.0 2017-11-02
3.6.0 2017-09-25
3.5.0 2017-08-10
3.4.3 2017-05-30
3.4.2 2017-05-18
3.4.1 2017-04-27
3.4.0 2017-04-21
3.3.3 2017-04-04
3.3.2 2017-04-04
3.3.1 2017-03-13
3.3.0 2017-03-08
3.0.2 2016-11-28
3.0.1 2016-10-14
3.0.0 2016-10-13
3.0.0b3 2016-09-14
3.0.0b2 2016-08-02
3.0.0b1 2016-07-29
2.53.6 2016-06-28
2.53.5 2016-06-10
2.53.4 2016-06-08
2.53.3 2016-06-08
2.53.2 2016-04-19
2.53.1 2016-03-16
2.53.0 2016-03-15
2.52.0 2016-02-11
2.51.1 2016-02-05
2.51.0 2016-02-05
2.50.1 2016-02-01
2.50.0 2016-01-27
2.49.2 2016-01-19
2.49.1 2016-01-18
2.49.0 2016-01-13
2.48.0 2015-10-07
2.47.3 2015-09-15
2.47.2 2015-09-15
2.47.1 2015-08-10
2.47.0 2015-07-29
2.46.1 2015-07-20
2.46.0 2015-06-04
2.45.0 2015-02-27
2.44.0 2014-10-23
2.43.0 2014-09-09
2.42.1 2014-05-27
2.42.0 2014-05-22
2.41.0 2014-03-28
2.40.0 2014-02-19
2.39.0 2013-12-16
2.38.4 2013-12-12
2.38.3 2013-12-09
2.38.2 2013-12-09
2.38.1 2013-12-05
2.38.0 2013-12-05
2.37.2 2013-10-22
2.37.1 2013-10-21
2.37.0 2013-10-18
2.36.0 2013-10-17
2.35.0 2013-08-13
2.34.0 2013-08-06
2.33.0 2013-05-22
2.32.0 2013-04-10
2.31.0 2013-02-27
2.30.0 2013-02-19
2.29.0 2013-01-17
2.28.0 2012-12-11
2.27.0 2012-12-05
2.26.0 2012-11-01
2.25.0 2012-07-18
2.24.0 2012-06-19
2.23.0 2012-06-07
2.22.1 2012-05-29
2.22.0 2012-05-29
2.21.3 2012-05-08
2.21.2 2012-04-16
2.21.1 2012-04-11
2.21.0 2012-04-11
2.20.0 2012-02-27
2.19.1 2012-02-08
2.19.0 2012-02-08
2.18.1 2012-01-27
2.17.0 2012-01-16
2.16.0 2012-01-04
2.15.0 2011-12-08
2.14.0 2011-11-29
2.13.1 2011-11-18
2.13.0 2011-11-18
2.12.1 2011-11-10
2.12.0 2011-11-10
2.11.1 2011-10-28
2.11.0 2011-10-28
2.10.0 2011-10-28
2.9.0 2011-10-21
2.8.1 2011-10-06
2.8.0 2011-10-06
2.7.0 2011-09-26
2.6.0 2011-09-13
2.5.0 2011-08-23
2.4.0 2011-08-11
2.3.0 2011-08-01
2.2.0 2011-07-26
2.1.0 2011-07-18
2.0.1 2011-07-08
2.0.0 2011-07-08
2.0-dev-9429 2010-08-05
2.0-dev-9341 2010-07-17
2.0-dev-9340 2010-07-17
2.0-dev-9338 2010-07-16
2.0-dev-9310 2010-07-16
2.0-dev-9307 2010-07-10
2.0-dev-9306 2010-07-10
2.0-dev-9284 2010-07-07
2.0-dev-9231 2010-06-30
2.0-dev-9212 2010-06-25
2.0-dev-9138 2010-06-12
2.0-dev 2010-06-08
2.0rc3 2011-06-20
2.0rc2 2011-06-02
2.0rc1 2011-06-01
2.0dev6 2011-02-14
2.0dev5 2011-02-14
2.0dev4 2011-02-14
2.0dev3 2011-02-14
2.0dev2 2011-02-14
2.0dev1 2011-02-14
2.0b4dev 2011-04-14
2.0b3dev 2011-03-21
2.0b3 2011-03-21
2.0b2 2011-02-14
2.0a5 2010-08-13
1.0.3 2010-05-26
1.0.1 2009-06-17
0.9.2 2008-04-25