webdriver.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  19. from .service import Service
  20. class WebDriver(RemoteWebDriver):
  21. """
  22. Wrapper to communicate with PhantomJS through Ghostdriver.
  23. You will need to follow all the directions here:
  24. https://github.com/detro/ghostdriver
  25. """
  26. def __init__(self, executable_path="phantomjs",
  27. port=0, desired_capabilities=DesiredCapabilities.PHANTOMJS,
  28. service_args=None, service_log_path=None):
  29. """
  30. Creates a new instance of the PhantomJS / Ghostdriver.
  31. Starts the service and then creates new instance of the driver.
  32. :Args:
  33. - executable_path - path to the executable. If the default is used it assumes the executable is in the $PATH
  34. - port - port you would like the service to run, if left as 0, a free port will be found.
  35. - desired_capabilities: Dictionary object with non-browser specific
  36. capabilities only, such as "proxy" or "loggingPref".
  37. - service_args : A List of command line arguments to pass to PhantomJS
  38. - service_log_path: Path for phantomjs service to log to.
  39. """
  40. self.service = Service(
  41. executable_path,
  42. port=port,
  43. service_args=service_args,
  44. log_path=service_log_path)
  45. self.service.start()
  46. try:
  47. RemoteWebDriver.__init__(
  48. self,
  49. command_executor=self.service.service_url,
  50. desired_capabilities=desired_capabilities)
  51. except Exception:
  52. self.quit()
  53. raise
  54. self._is_remote = False
  55. def quit(self):
  56. """
  57. Closes the browser and shuts down the PhantomJS executable
  58. that is started when starting the PhantomJS
  59. """
  60. try:
  61. RemoteWebDriver.quit(self)
  62. except Exception:
  63. # We don't care about the message because something probably has gone wrong
  64. pass
  65. finally:
  66. self.service.stop()