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 Reference: |
Updated documentation published with each commit is available at:readthedocs.io
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 withpip <https://pip.pypa.io/>.
Latest official release:
pipinstall-Uselenium
Nightly development release:
pipinstall-U--index-urlhttps://test.pypi.org/simple/--extra-index-urlhttps://pypi.org/simple/selenium
Note: you should consider using avirtual environmentto 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 thedriver executable was available on your systemPATH, or specified explicitly in code. Modern versions of Seleniumhandle browser and driver installation for you withSelenium Manager. You generally don’t have to worry aboutdriver installation or configuration now that it’s done for you when you instantiate a WebDriver. Selenium Manager workswith most supported platforms and browsers. If it doesn’t meet your needs, you can still install and specify browsersand 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’sstandardunittest 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 a 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
Clone your fork locally
- Create a branch for your work
git checkout -b my-cool-branch-name
- Create a virtual environment and install tox
python -m venv venv && source venv/bin/activate && pip install tox
Make your changes
- Run the linter/formatter
tox -e linting
If tox exits0, commit and push. Otherwise, fix the newly introduced style violations
Submit a Pull Request