Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.6k
Python Bindings
Selenium officially supports python 2.7 and 3.6, however other versions should also work.
The latest official release is available on thePython Package Index. It's a good practice to install python packages intovirtual environments rather than in your global site packages. To install this usingpip, run the following command:
pip install seleniumImporting the module is performed by entering the following in your python shell:
fromseleniumimportwebdriverdriver=webdriver.Firefox()
fromseleniumimportwebdriverdriver=webdriver.Chrome()
fromseleniumimportwebdriverdriver=webdriver.Remote(browser_name="firefox",platform="any")
fromseleniumimportwebdriverdriver=webdriver.Ie()
fromseleniumimportwebdriverdriver=webdriver.Edge()
fromseleniumimportwebdriverdriver=webdriver.Safari()
The documentation is available onlinehere and can by built locally using the commandtox -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__)
Wherefunctionname 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 pythontitle is the equivalent ofgetTitle() in Java.
To reflect pythonic behavior of flat object hierarchies the python bindings e.g.find_element_by_xpath("//h1") rather thanfindElement(By.xpath("//h1")); but it does give you the freedom of doingfind_element(by=By.XPATH, value='//h1')
To install the latest unreleased version,clonehttps://github.com/SeleniumHQ/selenium and run the following commands from the repository root directory:
./go py_prep_for_install_releasecd pypython setup.py installCode should adhere toFlake8 with the exception ofE501.
Docstrings should adhere toPEP257. Arguments, return values, exceptions, etc should usereStructuredText.
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 installTox.
Note that you will either need to change to the
pysubdirectory when runningtox, or point to the configuration file usingtox -c py/tox.ini.
By default, runningtox will attempt to execute all of the defined environments. This means the tests for python 2.7 and 3.6 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, runtox -l, and to execute a single environment, usetox -e.
As an example, this command will run the tests for Firefox against python 2.7:
tox -e py27-marionetteThe tests are executed usingpytest, 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-marionette -- py/test/selenium/webdriver/common/visibility_tests.pyTo run a single test, you can use the keyword filter, such as:
tox -e py27-marionette -- -k testShouldShowElementNotVisibleWithHiddenAttributeUnfortunately, there will be some tests that are expected to fail due to known issues. You can mark these tests using thestandard pytest methods, however if the test uses thedriver 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):
importpytest@pytest.mark.xfail_chrome@pytest.mark.xfail_firefoxdeftest_something(driver):assertsomethingisTrue
All of the same arguments from pytest'sxfail mark are available to these extended marks. Wherever possible you should provide areason with a reference to the raised issue/bug. If the test raises an unexpected exception you should also provide theraises 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 thecondition 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:
importsysimportpytestfromselenium.common.exceptionsimportWebDriverException@pytest.mark.xfail_firefox(condition=sys.platform=='darwin',reason='https://myissuetracker.com/issue?id=1234',raises=WebDriverExceptiondeftest_something(driver):assertsomethingisTrue
You should avoid usingimperative xfail as these will never allow the test an opportunity to unexpectedly pass (when the issue is resolved).
We also recommend against usingskip unless there is good reason. If your test failure causes a hang or some other undesirable side-effect you can passrun=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-marionette -- -m xfail_marionette --runxfailTo 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 andchange log. The version number is in the form ofX.Y.Z, whereX.Y is taken from the latestGitHub release, andZ increments for each release.
When you're ready, the release can be made by running the following command:
./go py_releaseThis wiki is not where you want to be!Visit the Wiki Home for more useful links
Getting Involved
Triaging Issues
Releasing Selenium
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 toOfficial 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