Selenium Client Driver¶
Introduction¶
Python language bindings for Selenium WebDriver.
Theselenium package is used to automate web browser interaction from Python.
Home: | |
GitHub: | |
PyPI: | |
IRC/Slack: | |
Docs: | |
API Docs: |
Updated documentation published with each commit is available at:readthedocs.io
Supported Python Versions¶
Python 3.9+
Supported Browsers¶
Several browsers are supported, as well as the Remote protocol:
Chrome
Edge
Firefox
Safari
WebKitGTK
WPEWebKit
Installing¶
If you havepip on your system, you can simply install or upgrade the Python bindings:
pipinstall-Uselenium
You may want to consider using avirtual environmentto create isolated Python environments.
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 driverexecutable was available on your systemPATH, or specified explicitly in code. Modern versions of Selenium handle browser anddriver installation for you withSelenium Manager. You generallydon’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 andspecify browsers and drivers yourself.
Links to some of the more popular browser drivers:
Chrome: | |
Edge: | https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver |
Firefox: | |
Safari: |
Example 0:¶
launch a new Chrome browser
load a web page
close the browser
fromseleniumimportwebdriverdriver=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
fromseleniumimportwebdriverfromselenium.webdriver.common.byimportBydriver=webdriver.Chrome()driver.get('https://selenium.dev/documentation')assert'Selenium'indriver.titleelem=driver.find_element(By.ID,'m-documentationwebdriver')elem.click()assert'WebDriver'indriver.titledriver.quit()
Example 2:¶
Selenium WebDriver is often used as a basis for testing web applications. Here is a simple example using Python’s standardunittest library:
importunittestfromseleniumimportwebdriverclassGoogleTestCase(unittest.TestCase):defsetUp(self):self.driver=webdriver.Firefox()self.addCleanup(self.driver.quit)deftest_page_title(self):self.driver.get('https://www.google.com')self.assertIn('Google',self.driver.title)if__name__=='__main__':unittest.main(verbosity=2)
Selenium Grid (optional)¶
For local Selenium scripts, the Java server is not needed.
To use Selenium remotely, you need to also run the Selenium grid.For information on running Selenium Grid:https://www.selenium.dev/documentation/grid/getting_started/
To use Remote WebDriver see:https://www.selenium.dev/documentation/webdriver/drivers/remote_webdriver/?tab=python
Use The Source Luke!¶
View source code online:
Official: |
Contributing¶
Fork the selenium repo and clone it locally
- Create a branch for your work
Run:git checkout -b my-cool-branch-name
- Create a virtual environment and install tox
Run:python -m venv venv && source venv/bin/activate && pip install tox
Make your changes
Run:tox -e linting
- If tox exits0, commit and push. Otherwise, fix the newly introduced style violations
flake8 requires manual fixes
black will rewrite the violations automatically, however the files are unstaged and should be staged again
isort will rewrite the violations automatically, however the files are unstaged and should be staged again
Submit a Pull Request