Created
April 10, 2018 05:23
-
-
Save michaelfward/93c4526f9839f3f041955de88beacc7a to your computer and use it in GitHub Desktop.
Example of adding WebPage methods to PhantomJS webdriver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from selenium.webdriver import PhantomJS, DesiredCapabilities | |
from selenium.webdriver.remote.remote_connection import RemoteConnection as Remote | |
from selenium.webdriver.phantomjs.service import Service | |
from selenium.webdriver.remote.command import Command | |
import warnings | |
from selenium.webdriver.remote.webdriver import WebDriver as _RemoteWebDriver | |
class PhantomCommands(Command): | |
# this class exists PURELY for readability! | |
# you could (but shouldnt!) inline this string | |
# below, and the code will work exactly the same | |
EXECUTE_PHANTOM_SCRIPT = "executePhantomScript" | |
class RemoteConnection(Remote): | |
def __init__(self, *args, **kwargs): | |
super(Remote, self).__init__(*args, **kwargs) # init ourselves first, so we have _commands | |
self._commands.update({ # update our command dict with the phantom command | |
PhantomCommands.EXECUTE_PHANTOM_SCRIPT: ("POST", "/session/$sessionId/phantom/execute")}) | |
class PhantomWebDriver(PhantomJS): | |
def __init__(self, executable_path="phantomjs", | |
port=0, desired_capabilities=DesiredCapabilities.PHANTOMJS, | |
service_args=None, service_log_path=None): | |
""" | |
Creates a new instance of the PhantomJS / Ghostdriver. | |
Starts the service and then creates new instance of the driver. | |
:Args: | |
- executable_path - path to the executable. If the default is used it assumes the executable is in the $PATH | |
- port - port you would like the service to run, if left as 0, a free port will be found. | |
- desired_capabilities: Dictionary object with non-browser specific | |
capabilities only, such as "proxy" or "loggingPref". | |
- service_args : A List of command line arguments to pass to PhantomJS | |
- service_log_path: Path for phantomjs service to log to. | |
""" | |
warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless ' | |
'versions of Chrome or Firefox instead') | |
self.service = Service( | |
executable_path, | |
port=port, | |
service_args=service_args, | |
log_path=service_log_path) | |
self.service.start() | |
try: | |
_RemoteWebDriver.__init__( | |
self, | |
command_executor=RemoteConnection(self.service.service_url), | |
desired_capabilities=desired_capabilities) | |
except Exception: | |
self.quit() | |
raise | |
self._is_remote = False | |
def render_pdf(self, file_name): | |
script = "this.render('%s')" % file_name | |
args = [] | |
return self.execute(PhantomCommands.EXECUTE_PHANTOM_SCRIPT, {"script": script, "args": args}) | |
if __name__ == '__main__': | |
import sys | |
driver = PhantomWebDriver() | |
driver.get("http://mwarddev.com/blog/adding-missing-phantomjs-bindings-to-selenium-webdriver/") | |
print driver.render_pdf("post.pdf") | |
driver.quit() | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment