Alexei Barantsev | 70795f3 | 2019-07-26 15:48:34 | [diff] [blame] | 1 | ====================== |
| 2 | Selenium Client Driver |
| 3 | ====================== |
| 4 | |
| 5 | Introduction |
| 6 | ============ |
| 7 | |
| 8 | Python language bindings for Selenium WebDriver. |
| 9 | |
| 10 | The `selenium` package is used to automate web browser interaction from Python. |
| 11 | |
Diego Molina | 10f1cd8 | 2023-06-08 12:27:23 | [diff] [blame] | 12 | +-----------------+--------------------------------------------------------------------------------------+ |
| 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 Barantsev | 70795f3 | 2019-07-26 15:48:34 | [diff] [blame] | 21 | |
| 22 | Several browsers/drivers are supported (Firefox, Chrome, Internet Explorer), as well as the Remote protocol. |
| 23 | |
| 24 | Supported Python Versions |
| 25 | ========================= |
| 26 | |
Simon K | 709425a | 2023-08-28 08:11:48 | [diff] [blame] | 27 | * Python 3.8+ |
Alexei Barantsev | 70795f3 | 2019-07-26 15:48:34 | [diff] [blame] | 28 | |
| 29 | Installing |
| 30 | ========== |
| 31 | |
| 32 | If 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 | |
titusfortner | a3b7320 | 2023-08-31 16:02:13 | [diff] [blame] | 36 | Alternately, you can download the source distribution from `PyPI <https://pypi.org/project/selenium/#files>`, unarchive it, and run:: |
Alexei Barantsev | 70795f3 | 2019-07-26 15:48:34 | [diff] [blame] | 37 | |
| 38 | python setup.py install |
| 39 | |
| 40 | Note: You may want to consider using `virtualenv <http://www.virtualenv.org/>`_ to create isolated Python environments. |
| 41 | |
| 42 | Drivers |
| 43 | ======= |
| 44 | |
| 45 | Selenium requires a driver to interface with the chosen browser. Firefox, |
| 46 | for 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 | |
| 48 | Failure to observe this step will give you an error `selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.` |
| 49 | |
| 50 | Other supported browsers will have their own drivers available. Links to some of the more popular browser drivers follow. |
| 51 | |
| 52 | +--------------+-----------------------------------------------------------------------+ |
Patrick Beart | 6f4a383 | 2019-10-02 16:37:29 | [diff] [blame] | 53 | | **Chrome**: | https://chromedriver.chromium.org/downloads | |
Alexei Barantsev | 70795f3 | 2019-07-26 15:48:34 | [diff] [blame] | 54 | +--------------+-----------------------------------------------------------------------+ |
| 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 | |
| 62 | Example 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 Molina | 0220cac | 2019-11-16 20:28:00 | [diff] [blame] | 73 | browser.get('http://selenium.dev/') |
Alexei Barantsev | 70795f3 | 2019-07-26 15:48:34 | [diff] [blame] | 74 | |
| 75 | Example 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 |
AutomatedTester | 21a38a9 | 2020-04-07 20:39:10 | [diff] [blame] | 86 | from selenium.webdriver.common.by import By |
Alexei Barantsev | 70795f3 | 2019-07-26 15:48:34 | [diff] [blame] | 87 | 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 | |
AutomatedTester | 21a38a9 | 2020-04-07 20:39:10 | [diff] [blame] | 94 | elem = browser.find_element(By.NAME, 'p') # Find the search box |
Alexei Barantsev | 70795f3 | 2019-07-26 15:48:34 | [diff] [blame] | 95 | elem.send_keys('seleniumhq' + Keys.RETURN) |
| 96 | |
| 97 | browser.quit() |
| 98 | |
| 99 | Example 2: |
| 100 | ========== |
| 101 | |
| 102 | Selenium 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 K | dabb09e | 2022-04-07 17:22:23 | [diff] [blame] | 115 | def test_page_title(self): |
Alexei Barantsev | 70795f3 | 2019-07-26 15:48:34 | [diff] [blame] | 116 | 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 | |
| 122 | Selenium Server (optional) |
| 123 | ========================== |
| 124 | |
| 125 | For normal WebDriver scripts (non-Remote), the Java server is not needed. |
| 126 | |
Diego Molina | b38583d | 2023-06-08 09:27:47 | [diff] [blame] | 127 | However, to use Selenium Webdriver Remote , you need to also run the Selenium server. The server requires a Java Runtime Environment (JRE). |
Alexei Barantsev | 70795f3 | 2019-07-26 15:48:34 | [diff] [blame] | 128 | |
Diego Molina | 1f08d92 | 2021-08-20 15:32:30 | [diff] [blame] | 129 | Download the server separately, from: https://www.selenium.dev/downloads/ |
Alexei Barantsev | 70795f3 | 2019-07-26 15:48:34 | [diff] [blame] | 130 | |
| 131 | Run the server from the command line:: |
| 132 | |
titusfortner | 553c663 | 2023-11-02 16:24:22 | [diff] [blame] | 133 | java -jar selenium-server-4.15.0.jar |
Alexei Barantsev | 70795f3 | 2019-07-26 15:48:34 | [diff] [blame] | 134 | |
| 135 | Then run your Python client scripts. |
| 136 | |
| 137 | Use The Source Luke! |
| 138 | ==================== |
| 139 | |
| 140 | View source code online: |
| 141 | |
John F. Douthat | f535154 | 2020-07-15 20:54:25 | [diff] [blame] | 142 | +-----------+------------------------------------------------------+ |
Diego Molina | 10f1cd8 | 2023-06-08 12:27:23 | [diff] [blame] | 143 | | Official: | https://github.com/SeleniumHQ/selenium/tree/trunk/py | |
John F. Douthat | f535154 | 2020-07-15 20:54:25 | [diff] [blame] | 144 | +-----------+------------------------------------------------------+ |
symonk | bbce33d | 2022-10-01 12:43:41 | [diff] [blame] | 145 | |
symonk | bbce33d | 2022-10-01 12:43:41 | [diff] [blame] | 146 | Contributing |
| 147 | ============= |
| 148 | |
| 149 | - Create a branch for your work |
| 150 | - Ensure `tox` is installed (using a `virtualenv` is recommended) |
Simon K | 709425a | 2023-08-28 08:11:48 | [diff] [blame] | 151 | - `python3.8 -m venv .venv && . .venv/bin/activate && pip install tox` |
symonk | bbce33d | 2022-10-01 12:43:41 | [diff] [blame] | 152 | - 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. |