Skip to content

Instantly share code, notes, and snippets.

@Markbnj
Created August 11, 2015 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Markbnj/c22f45afb7db53fb5cc0 to your computer and use it in GitHub Desktop.
Save Markbnj/c22f45afb7db53fb5cc0 to your computer and use it in GitHub Desktop.
Set up phantomjs driver for selenium, example
# An example showing how to set up phantomjs for use with Selenium
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
# These arguments will be passed on the phantomjs command line
# when webdriver launches it. See the reference page at
# http://phantomjs.org/api/command-line.html for more settings.
#
# Uncomment the proxy settings and change the values appropriately
# to get phantom to use a proxy (i.e. to examine requests).
PHANTOMJS_ARGS = [
# "--proxy=<proxy address>:<proxy port>",
# "--proxy-type=http,https",
"--load-images=false",
"--ignore-ssl-errors=true",
"--ssl-protocol=any"
]
# Set the page download timeout here
DOWNLOAD_TIMEOUT = 5
def _setup_phantom_caps(self):
"""
Creates the device capabilities structure.
"""
dcaps = dict(DesiredCapabilities.PHANTOMJS)
dcaps["phantomjs.page.settings.userAgent"] = getattr(settings, "USER_AGENT")
dcaps["phantomjs.page.settings.webSecurityEnabled"] = False
dcaps["phantomjs.page.settings.javascriptCanOpenWindows"] = False
dcaps["phantomjs.page.settings.javascriptCanCloseWindows"] = False
dcaps["takesScreenshot"] = False
return dcaps
dcaps = self._setup_phantom_caps()
# copy this list, because for some reason webdriver
# modifies the original in-memory
service_args = list(PHANTOMJS_ARGS)
log_path = "<Where the log should be>"
driver = webdriver.PhantomJS(
desired_capabilities=dcaps,
service_args=service_args,
service_log_path=log_path)
driver.set_page_load_timeout(DOWNLOAD_TIMEOUT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment