-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Python Bindings
The python bindings for Selenium 2 are now available. The bindings include the full functionality of Selenium 1 and 2 (WebDriver). The package currently supports the Remote, Firefox, Chrome and IE protocols natively.
note : Currently Selenium only supports Python 2.6, 2.7, 3.2, 3.3
If using selenium 1, before attempting to run a test, be sure to download the Selenium Server jar file, and run it via
java -jar selenium-server-standalone.jar
in your terminal/cmd prompt, before attempting to run a test. Selenium 2 however, does not require the jar file.
Currently, there are two versions of Selenium available for use.
The first version, is the latest official release, which is available on the Python Package Index http://python.org/pypi/selenium. To use this version, in your terminal type:
[sudo] easy_install selenium
or [sudo] pip install selenium
The second version, is the current code from trunk. To use this, checkout the trunk repository at https://github.com/SeleniumHQ/selenium. After the download is completed, cd to the root of the downloaded directory via terminal/cmd prompt. Perform the following commands:
./go py_prep_for_install_release
python setup.py install
Upon completion, the package should be installed successfully.
One advantage of using trunk as of writing, is the reorganization of the package. Previously, to initialize a browser you had to perform,
from selenium.firefox.webdriver import WebDriver
driver = WebDriver()
This has been changed, so now all that is required is:
from selenium import webdriver
driver = webdriver.Firefox()
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
Depending on the driver you wish to utilize, importing the module is performed by entering the following in your python shell:
Selenium 1:
from selenium import selenium
Selenium 2:
from selenium import webdriver
To read the API documentation of the Python Bindings go to the Python bindings API doc page.
Alternatively use your python shell to view all commands available to you, after importing perform:
Selenium 1:
dir(selenium)
Selenium 2:
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')
All of the browsers supported by the Java implementation of Selenium are available in the Python bindings. For example:
from selenium import selenium
selenium = selenium("localhost", 4444, "*iexplore", "http://google.com/")
selenium.start()
from selenium import selenium
selenium = selenium("localhost", 4444, "*firefox", "http://google.com/")
selenium.start()
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()
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