DESCRIPTION.rst 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. ======================
  2. Selenium Client Driver
  3. ======================
  4. Introduction
  5. ============
  6. Python language bindings for Selenium WebDriver.
  7. The `selenium` package is used to automate web browser interaction from Python.
  8. +-----------+--------------------------------------------------------------------------------------+
  9. | **Home**: | http://www.seleniumhq.org |
  10. +-----------+--------------------------------------------------------------------------------------+
  11. | **Docs**: | `selenium package API <https://seleniumhq.github.io/selenium/docs/api/py/api.html>`_ |
  12. +-----------+--------------------------------------------------------------------------------------+
  13. | **Dev**: | https://github.com/SeleniumHQ/Selenium |
  14. +-----------+--------------------------------------------------------------------------------------+
  15. | **PyPI**: | https://pypi.python.org/pypi/selenium |
  16. +-----------+--------------------------------------------------------------------------------------+
  17. | **IRC**: | **#selenium** channel on freenode |
  18. +-----------+--------------------------------------------------------------------------------------+
  19. Several browsers/drivers are supported (Firefox, Chrome, Internet Explorer, PhantomJS), as well as the Remote protocol.
  20. Supported Python Versions
  21. =========================
  22. * Python 2.6, 2.7
  23. * Python 3.3+
  24. Installing
  25. ==========
  26. If you have `pip <https://pip.pypa.io/>`_ on your system, you can simply install or upgrade the Python bindings::
  27. pip install -U selenium
  28. Alternately, you can download the source distribution from `PyPI <http://pypi.python.org/pypi/selenium>`_ (e.g. selenium-3.4.3.tar.gz), unarchive it, and run::
  29. python setup.py install
  30. Note: both of the methods described above install `selenium` as a system-wide package That will require administrative/root access to their machine. You may consider using a `virtualenv <http://www.virtualenv.org/>`_ to create isolated Python environments instead.
  31. Drivers
  32. =======
  33. Selenium requires a driver to interface with the chosen browser. Firefox,
  34. 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`.
  35. Failure to observe this step will give you an error `selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.`
  36. Other supported browsers will have their own drivers available. Links to some of the more popular browser drivers follow.
  37. +--------------+-----------------------------------------------------------------------+
  38. | **Chrome**: | https://sites.google.com/a/chromium.org/chromedriver/downloads |
  39. +--------------+-----------------------------------------------------------------------+
  40. | **Edge**: | https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/ |
  41. +--------------+-----------------------------------------------------------------------+
  42. | **Firefox**: | https://github.com/mozilla/geckodriver/releases |
  43. +--------------+-----------------------------------------------------------------------+
  44. | **Safari**: | https://webkit.org/blog/6900/webdriver-support-in-safari-10/ |
  45. +--------------+-----------------------------------------------------------------------+
  46. Example 0:
  47. ==========
  48. * open a new Firefox browser
  49. * load the page at the given URL
  50. ::
  51. from selenium import webdriver
  52. browser = webdriver.Firefox()
  53. browser.get('http://seleniumhq.org/')
  54. Example 1:
  55. ==========
  56. * open a new Firefox browser
  57. * load the Yahoo homepage
  58. * search for "seleniumhq"
  59. * close the browser
  60. ::
  61. from selenium import webdriver
  62. from selenium.webdriver.common.keys import Keys
  63. browser = webdriver.Firefox()
  64. browser.get('http://www.yahoo.com')
  65. assert 'Yahoo!' in browser.title
  66. elem = browser.find_element_by_name('p') # Find the search box
  67. elem.send_keys('seleniumhq' + Keys.RETURN)
  68. browser.quit()
  69. Example 2:
  70. ==========
  71. Selenium WebDriver is often used as a basis for testing web applications. Here is a simple example uisng Python's standard `unittest <http://docs.python.org/3/library/unittest.html>`_ library:
  72. ::
  73. import unittest
  74. class GoogleTestCase(unittest.TestCase):
  75. def setUp(self):
  76. self.browser = webdriver.Firefox()
  77. self.addCleanup(self.browser.quit)
  78. def testPageTitle(self):
  79. self.browser.get('http://www.google.com')
  80. self.assertIn('Google', self.browser.title)
  81. if __name__ == '__main__':
  82. unittest.main(verbosity=2)
  83. Selenium Server (optional)
  84. ==========================
  85. For normal WebDriver scripts (non-Remote), the Java server is not needed.
  86. However, to use Selenium Webdriver Remote or the legacy Selenium API (Selenium-RC), you need to also run the Selenium server. The server requires a Java Runtime Environment (JRE).
  87. Download the server separately, from: http://selenium-release.storage.googleapis.com/3.4/selenium-server-standalone-3.4.0.jar
  88. Run the server from the command line::
  89. java -jar selenium-server-standalone-3.4.0.jar
  90. Then run your Python client scripts.
  91. Use The Source Luke!
  92. ====================
  93. View source code online:
  94. +-----------+-------------------------------------------------------+
  95. | official: | https://github.com/SeleniumHQ/selenium/tree/master/py |
  96. +-----------+-------------------------------------------------------+