blob: 1adc92dc5ccc3ee8bdd2782c6aa420fcb5856ee3 [file] [log] [blame]
Alexei Barantsev70795f32019-07-26 15:48:341======================
2Selenium Client Driver
3======================
4
5Introduction
6============
7
8Python language bindings for Selenium WebDriver.
9
10The `selenium` package is used to automate web browser interaction from Python.
11
Diego Molina10f1cd82023-06-08 12:27:2312+-----------------+--------------------------------------------------------------------------------------+
13| **Home**: | https://selenium.dev |
14+-----------------+--------------------------------------------------------------------------------------+
15| **GitHub**: | https://github.com/SeleniumHQ/Selenium |
16+-----------------+--------------------------------------------------------------------------------------+
17| **PyPI**: | https://pypi.org/project/selenium/ |
18+-----------------+--------------------------------------------------------------------------------------+
19| **IRC/Slack**: | `Selenium chat room <https://www.selenium.dev/support/#ChatRoom>`_ |
20+-----------------+--------------------------------------------------------------------------------------+
Alexei Barantsev70795f32019-07-26 15:48:3421
22Several browsers/drivers are supported (Firefox, Chrome, Internet Explorer), as well as the Remote protocol.
23
24Supported Python Versions
25=========================
26
Simon K709425a2023-08-28 08:11:4827* Python 3.8+
Alexei Barantsev70795f32019-07-26 15:48:3428
29Installing
30==========
31
32If you have `pip <https://pip.pypa.io/>`_ on your system, you can simply install or upgrade the Python bindings::
33
34 pip install -U selenium
35
titusfortnera3b73202023-08-31 16:02:1336Alternately, you can download the source distribution from `PyPI <https://pypi.org/project/selenium/#files>`, unarchive it, and run::
Alexei Barantsev70795f32019-07-26 15:48:3437
38 python setup.py install
39
40Note: You may want to consider using `virtualenv <http://www.virtualenv.org/>`_ to create isolated Python environments.
41
42Drivers
43=======
44
45Selenium requires a driver to interface with the chosen browser. Firefox,
46for example, requires `geckodriver <https://github.com/mozilla/geckodriver/releases>`_, which needs to be installed before the below examples can be run. Make sure it's in your `PATH`, e. g., place it in `/usr/bin` or `/usr/local/bin`.
47
48Failure to observe this step will give you an error `selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.`
49
50Other supported browsers will have their own drivers available. Links to some of the more popular browser drivers follow.
51
52+--------------+-----------------------------------------------------------------------+
Patrick Beart6f4a3832019-10-02 16:37:2953| **Chrome**: | https://chromedriver.chromium.org/downloads |
Alexei Barantsev70795f32019-07-26 15:48:3454+--------------+-----------------------------------------------------------------------+
55| **Edge**: | https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/ |
56+--------------+-----------------------------------------------------------------------+
57| **Firefox**: | https://github.com/mozilla/geckodriver/releases |
58+--------------+-----------------------------------------------------------------------+
59| **Safari**: | https://webkit.org/blog/6900/webdriver-support-in-safari-10/ |
60+--------------+-----------------------------------------------------------------------+
61
62Example 0:
63==========
64
65* open a new Firefox browser
66* load the page at the given URL
67
68.. code-block:: python
69
70 from selenium import webdriver
71
72 browser = webdriver.Firefox()
Diego Molina0220cac2019-11-16 20:28:0073 browser.get('http://selenium.dev/')
Alexei Barantsev70795f32019-07-26 15:48:3474
75Example 1:
76==========
77
78* open a new Firefox browser
79* load the Yahoo homepage
80* search for "seleniumhq"
81* close the browser
82
83.. code-block:: python
84
85 from selenium import webdriver
AutomatedTester21a38a92020-04-07 20:39:1086 from selenium.webdriver.common.by import By
Alexei Barantsev70795f32019-07-26 15:48:3487 from selenium.webdriver.common.keys import Keys
88
89 browser = webdriver.Firefox()
90
91 browser.get('http://www.yahoo.com')
92 assert 'Yahoo' in browser.title
93
AutomatedTester21a38a92020-04-07 20:39:1094 elem = browser.find_element(By.NAME, 'p') # Find the search box
Alexei Barantsev70795f32019-07-26 15:48:3495 elem.send_keys('seleniumhq' + Keys.RETURN)
96
97 browser.quit()
98
99Example 2:
100==========
101
102Selenium WebDriver is often used as a basis for testing web applications. Here is a simple example using Python's standard `unittest <http://docs.python.org/3/library/unittest.html>`_ library:
103
104.. code-block:: python
105
106 import unittest
107 from selenium import webdriver
108
109 class GoogleTestCase(unittest.TestCase):
110
111 def setUp(self):
112 self.browser = webdriver.Firefox()
113 self.addCleanup(self.browser.quit)
114
Simon Kdabb09e2022-04-07 17:22:23115 def test_page_title(self):
Alexei Barantsev70795f32019-07-26 15:48:34116 self.browser.get('http://www.google.com')
117 self.assertIn('Google', self.browser.title)
118
119 if __name__ == '__main__':
120 unittest.main(verbosity=2)
121
122Selenium Server (optional)
123==========================
124
125For normal WebDriver scripts (non-Remote), the Java server is not needed.
126
Diego Molinab38583d2023-06-08 09:27:47127However, to use Selenium Webdriver Remote , you need to also run the Selenium server. The server requires a Java Runtime Environment (JRE).
Alexei Barantsev70795f32019-07-26 15:48:34128
Diego Molina1f08d922021-08-20 15:32:30129Download the server separately, from: https://www.selenium.dev/downloads/
Alexei Barantsev70795f32019-07-26 15:48:34130
131Run the server from the command line::
132
titusfortner553c6632023-11-02 16:24:22133 java -jar selenium-server-4.15.0.jar
Alexei Barantsev70795f32019-07-26 15:48:34134
135Then run your Python client scripts.
136
137Use The Source Luke!
138====================
139
140View source code online:
141
John F. Douthatf5351542020-07-15 20:54:25142+-----------+------------------------------------------------------+
Diego Molina10f1cd82023-06-08 12:27:23143| Official: | https://github.com/SeleniumHQ/selenium/tree/trunk/py |
John F. Douthatf5351542020-07-15 20:54:25144+-----------+------------------------------------------------------+
symonkbbce33d2022-10-01 12:43:41145
symonkbbce33d2022-10-01 12:43:41146Contributing
147=============
148
149 - Create a branch for your work
150 - Ensure `tox` is installed (using a `virtualenv` is recommended)
Simon K709425a2023-08-28 08:11:48151 - `python3.8 -m venv .venv && . .venv/bin/activate && pip install tox`
symonkbbce33d2022-10-01 12:43:41152 - After making changes, before committing execute `tox -e linting`
153 - If tox exits `0`, commit and push otherwise fix the newly introduced breakages.
154 - `flake8` requires manual fixes
155 - `black` will often rewrite the breakages automatically, however the files are unstaged and should staged again.
156 - `isort` will often rewrite the breakages automatically, however the files are unstaged and should staged again.