Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何通过GhostDriver(selenium)使用PhantomJS运行网页代码_Python_Selenium_Phantomjs_Ghostdriver - Fatal编程技术网

Python 如何通过GhostDriver(selenium)使用PhantomJS运行网页代码

Python 如何通过GhostDriver(selenium)使用PhantomJS运行网页代码,python,selenium,phantomjs,ghostdriver,Python,Selenium,Phantomjs,Ghostdriver,我正在寻找通过GhostDriver使用PhantomJS渲染pdf的能力,而不仅仅是渲染pdf。当我使用下一个代码时,页面正常加载: from selenium import webdriver driver = webdriver.PhantomJS('./node_modules/phantomjs/bin/phantomjs') driver.set_window_size(1024, 768) driver.get('http://stackoverflow.com') 当我通过命

我正在寻找通过
GhostDriver
使用
PhantomJS
渲染pdf的能力,而不仅仅是渲染pdf。当我使用下一个代码时,页面正常加载:

from selenium import webdriver

driver = webdriver.PhantomJS('./node_modules/phantomjs/bin/phantomjs')
driver.set_window_size(1024, 768)
driver.get('http://stackoverflow.com')
当我通过命令行使用下一个脚本时,pdf生成得非常完美

现在我想执行脚本,比如
rasterize.js
page.render('file.pdf')
),但要通过
webdriver
webdriver
具有
execute\u script
方法,但它看起来像
PhantomJS
代码求值,并且无法访问
webpage
实例上下文。另外,
webdriver
get\u screenshot\u as\u base64
方法,但它只返回png

我使用最新版本的
selenium
phantomjs
nodejs


因此,我的问题是如何通过
GhostDriver
访问
PhantomJS
网页实例并评估
render
方法?

有一种特殊的方法可以使用下一个命令从GhostDriver执行PhantomJS脚本:

POST /session/id/phantom/execute
它包含在中,因此它应该可以工作

看看这个例子:

def execute(script, args):
    driver.execute('executePhantomScript', {'script': script, 'args' : args })

driver = webdriver.PhantomJS('phantomjs')

# hack while the python interface lags
driver.command_executor._commands['executePhantomScript'] = ('POST', '/session/$sessionId/phantom/execute')

driver.get('http://stackoverflow.com')

# set page format
# inside the execution script, webpage is "this"
pageFormat = '''this.paperSize = {format: "A4", orientation: "portrait" };'''
execute(pageFormat, [])

# render current page
render = '''this.render("test.pdf")'''
execute(render, [])

请注意,在OS X PhantomJS中,由于OS X中Qt渲染引擎的限制(至少在PhantomJS v.1.9.8和更早版本中是如此),无法选择文本。

我也在寻找这一点。。。任何人?只要生成PDF(不是
GhostDriver
WebDriver
),您就可以使用
ghost.py
(需要QT),找到带有
pyexecjs
子流程的包装器。也存在python包,如
pdfkit
wkhtmltopdf
作为
wkhtmltopdf
的包装器-它应该具有相同的结果,因为webkit也是如此
weasyprint
也很好,但不是webkit。对于非python实现?在Java或python以外的任何其他实现中是否有这样做的方法?