webdriver.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Licensed to the Software Freedom Conservancy (SFC) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The SFC licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
  18. from .remote_connection import ChromeRemoteConnection
  19. from .service import Service
  20. from .options import Options
  21. class WebDriver(RemoteWebDriver):
  22. """
  23. Controls the ChromeDriver and allows you to drive the browser.
  24. You will need to download the ChromeDriver executable from
  25. http://chromedriver.storage.googleapis.com/index.html
  26. """
  27. def __init__(self, executable_path="chromedriver", port=0,
  28. chrome_options=None, service_args=None,
  29. desired_capabilities=None, service_log_path=None):
  30. """
  31. Creates a new instance of the chrome driver.
  32. Starts the service and then creates new instance of chrome driver.
  33. :Args:
  34. - executable_path - path to the executable. If the default is used it assumes the executable is in the $PATH
  35. - port - port you would like the service to run, if left as 0, a free port will be found.
  36. - desired_capabilities: Dictionary object with non-browser specific
  37. capabilities only, such as "proxy" or "loggingPref".
  38. - chrome_options: this takes an instance of ChromeOptions
  39. """
  40. if chrome_options is None:
  41. # desired_capabilities stays as passed in
  42. if desired_capabilities is None:
  43. desired_capabilities = self.create_options().to_capabilities()
  44. else:
  45. if desired_capabilities is None:
  46. desired_capabilities = chrome_options.to_capabilities()
  47. else:
  48. desired_capabilities.update(chrome_options.to_capabilities())
  49. self.service = Service(
  50. executable_path,
  51. port=port,
  52. service_args=service_args,
  53. log_path=service_log_path)
  54. self.service.start()
  55. try:
  56. RemoteWebDriver.__init__(
  57. self,
  58. command_executor=ChromeRemoteConnection(
  59. remote_server_addr=self.service.service_url),
  60. desired_capabilities=desired_capabilities)
  61. except Exception:
  62. self.quit()
  63. raise
  64. self._is_remote = False
  65. def launch_app(self, id):
  66. """Launches Chrome app specified by id."""
  67. return self.execute("launchApp", {'id': id})
  68. def quit(self):
  69. """
  70. Closes the browser and shuts down the ChromeDriver executable
  71. that is started when starting the ChromeDriver
  72. """
  73. try:
  74. RemoteWebDriver.quit(self)
  75. except Exception:
  76. # We don't care about the message because something probably has gone wrong
  77. pass
  78. finally:
  79. self.service.stop()
  80. def create_options(self):
  81. return Options()