def execute(script, args):
    driver.execute('executePhantomScript', {'script': script, 'args': args})
driver = webdriver.PhantomJS('phantomjs')
How to run webpage code with PhantomJS via GhostDriver (selenium)