-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Python Bindings
Selenium officially supports python 2.7 and 3.5, however other versions should also work.
The latest official release is available on the Python Package Index. It's a good practice to install python packages into virtual environments rather than in your global site packages. To install this using pip, run the following command:
pip install selenium
Importing the module is performed by entering the following in your python shell:
from selenium import webdriver
driver = webdriver.Firefox()
from selenium import webdriver
driver = webdriver.Chrome()
from selenium import webdriver
driver = webdriver.Remote(browser_name="firefox", platform="any")
from selenium import webdriver
driver = webdriver.Ie()
from selenium import webdriver
driver = webdriver.Edge()
from selenium import webdriver
driver = webdriver.Safari()
The documentation is available online here and can by built locally using the command tox -e docs
.
Alternatively use your python shell to view all commands available to you, after importing perform:
dir(webdriver)
To view the docstrings (documentation text attached to a function or method), perform
print('functionname'.__doc__)
Where functionname
is the function you wish to view more information on. For example,
print(selenium.open.__doc__)
Here is a summary of the major differences between the python and Java bindings.
Function names separate compound terms with underscores, rather than using Java's camelCase formatting. For example, in python title
is the equivalent of getTitle()
in Java.
To reflect pythonic behavior of flat object hierarchies the python bindings e.g. find_element_by_xpath("//h1")
rather than findElement(By.xpath("//h1"));
but it does give you the freedom of doing find_element(by=By.XPATH, value='//h1')
To install the latest unreleased version, clone https://github.com/SeleniumHQ/selenium and run the following commands from the repository root directory:
./go py_prep_for_install_release
python setup.py install
When developing Selenium, it is recommended you run the tests before and after making any changes to the code base. To perform these tests, you will first need to install Tox.
By default, running tox
will attempt to execute all of the defined environments. This means the tests for python 2.7 and 3.5 will run for each of the supported drivers. This is most likely not what you want, as some drivers will not run on certain platforms. It is therefore recommended that you specify the environments you wish to execute. To list all environments available, run tox -l
, and to execute a single environment, use tox -e
.
As an example, this command will run the tests for Firefox against python 2.7:
tox -e py27-firefox
The tests are executed using pytest, and you can pass positional arguments through Tox by specifying --
before them. In addition to other things, this allows you to filter tests. For example, to run a single test file:
tox -e py27-firefox -- py/test/selenium/webdriver/common/visibility_tests.py
To run a single test, you can use the keyword filter, such as:
tox -e py27-firefox -- -k testShouldShowElementNotVisibleWithHiddenAttribute
Unfortunately, there will be some tests that are expected to fail due to known issues. You can mark these tests using the standard pytest methods, however if the test uses the driver
fixture to run against multiple drivers, this will mark the tests for all of those drivers. If a test is only expected to fail in a subset of drivers, you can extend the xfail mark with the name of the driver. For example, to mark a test as expected to fail in Chrome and Firefox (but pass using any other driver):
import pytest
@pytest.mark.xfail_chrome
@pytest.mark.xfail_firefox
def test_something(driver):
assert something is True
All of the same arguments from pytest's xfail mark are available to these extended marks. Wherever possible you should provide a reason
with a reference to the raised issue/bug. If the test raises an unexpected exception you should also provide the raises
argument, as this will still cause a failure if the test starts failing for another reason.
If the expected failure is dependent on the platform, you should also include the condition
argument so that the test will be allowed to pass on other environments. For example, to mark a test as expected to fail when run against Firefox on macOS:
import sys
import pytest
from selenium.common.exceptions import WebDriverException
@pytest.mark.xfail_firefox(
condition=sys.platform == 'darwin',
reason='https://myissuetracker.com/issue?id=1234',
raises=WebDriverException
def test_something(driver):
assert something is True
You should avoid using imperative xfail as these will never allow the test an opportunity to unexpectedly pass (when the issue is resolved).
We also recommend against using skip unless there is good reason. If your test failure causes a hang or some other undesirable side-effect you can pass run=False
to the xfail mark.
To run expected failures locally, pass the --runxfail
command line option to pytest. If you want to run all expected failures for a specific driver you can do this by filtering on the xfail mark:
tox -e py27-firefox -- -m xfail_firefox --runxfail
To perform a release you will need to be a maintainer of the package on PyPI. Before pushing a new release you will need to update the version number and change log. The version number is in the form of X.Y.Z
, where X.Y
is taken from the latest GitHub release, and Z
increments for each release.
When you're ready, the release can be made by running the following command:
./go py_release
This wiki is not where you want to be! Visit the Wiki Home for more useful links
Getting Involved
Build Instructions
Releasing Selenium
Updating Chromium DevTools
Ruby Development
Python Bindings
Ruby Bindings
WebDriverJs
This content is being evaluated for where it belongs
Architectural Overview
Automation Atoms
HtmlUnitDriver
Lift Style API
LoadableComponent
Logging
PageFactory
RemoteWebDriver
Xpath In WebDriver
Moved to Official Documentation
Bot Style Tests
Buck
Continuous Integration
Crazy Fun Build
Design Patterns
Desired Capabilities
Developer Tips
Domain Driven Design
Firefox Driver
Firefox Driver Internals
Focus Stealing On Linux
Frequently Asked Questions
Google Summer Of Code
Grid Platforms
History
Internet Explorer Driver
InternetExplorerDriver Internals
Next Steps
PageObjects
RemoteWebDriverServer
Roadmap
Scaling WebDriver
SeIDE Release Notes
Selenium Emulation
Selenium Grid 4
Selenium Help
Shipping Selenium 3
The Team
TLC Meetings
Untrusted SSL Certificates
WebDriver For Mobile Browsers
Writing New Drivers