tbi*_*icr 8 python selenium phantomjs ghostdriver
我正在寻找能力渲染pdf与PhantomJSvia GhostDriver,而不仅仅是渲染pdf.当我使用下一个代码,然后正常加载页面:
from selenium import webdriver
driver = webdriver.PhantomJS('./node_modules/phantomjs/bin/phantomjs')
driver.set_window_size(1024, 768)
driver.get('http://stackoverflow.com')
Run Code Online (Sandbox Code Playgroud)
当我通过命令行https://github.com/ariya/phantomjs/blob/master/examples/rasterize.js使用下一个脚本时,pdf生成完美.
现在我想要执行脚本像rasterize.js(page.render('file.pdf'))但通过webdriver.webdriver有execute_script方法,但它看起来像PhantomJS代码评估,并且无权访问webpage实例上下文.也webdriver有get_screenshot_as_base64方法,但它只返回png.
我使用的最新版本selenium,phantomjs,nodejs.
所以我的问题是如何PhantomJS通过GhostDriver和评估render方法访问网页实例?
有一种特殊的方法可以使用下一个命令从GhostDriver执行PhantomJS脚本:
POST /session/id/phantom/execute
Run Code Online (Sandbox Code Playgroud)
它包含在GhostDriver v1.1.0中,所以它应该从PhantomJS v.1.9.6开始工作.
看看这个例子:
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, [])
Run Code Online (Sandbox Code Playgroud)
请注意,在OS X中,由于OS X中Qt渲染引擎的限制(至少使用PhantomJS v.1.9.8及更早版本),PhantomJS 将网页呈现为具有不可选文本的图像.
| 归档时间: |
|
| 查看次数: |
5269 次 |
| 最近记录: |