def execute(script, args):
    driver.execute('executePhantomScript', {'script': script, 'args': args})
driver = webdriver.PhantomJS('phantomjs')
driver.command_executor._commands['executePhantomScript'
    ] = 'POST', '/session/$sessionId/phantom/execute'
driver.get('http://stackoverflow.com')
pageFormat = 'this.paperSize = {format: "A4", orientation: "portrait" };'
Как запустить код веб-страницы с помощью PhantomJS через GhostDriver (selenium)