8

PhantomJSpdf をレンダリングするだけでなく、 viaでpdf をレンダリングする機能を探していGhostDriverます。次のコードを使用すると、ページが通常どおり読み込まれます。

from selenium import webdriver

driver = webdriver.PhantomJS('./node_modules/phantomjs/bin/phantomjs')
driver.set_window_size(1024, 768)
driver.get('http://stackoverflow.com')

コマンドラインhttps://github.com/ariya/phantomjs/blob/master/examples/rasterize.jsで次のスクリプトを使用すると、pdf が完全に生成されます。

rasterize.js今、私は( page.render('file.pdf'))のようなスクリプトを実行したいが、 webdriver. メソッドwebdriverを持っていますが、コード評価のように見え、インスタンス コンテキストにアクセスできません。メソッドもありますが、png のみを返します。execute_scriptPhantomJSwebpagewebdriverget_screenshot_as_base64

seleniumphantomjs、の最新バージョンを使用していますnodejs

では、私の質問は、メソッドPhantomJSを介して Web ページ インスタンスにアクセスしGhostDriver、評価renderする方法を教えてください。

4

1 に答える 1

9

次のコマンドを使用して、GhostDriver から PhantomJS スクリプトを実行する特別な方法があります。

POST /session/id/phantom/execute

これは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, [])

OS X では、OS Xの Qt レンダリング エンジンの制限により (少なくとも PhantomJS v.1.9.8 以前では)、 PhantomJSは Web ページを選択できないテキストを含む画像としてレンダリングすることに注意してください。

于 2015-02-01T23:15:19.473 に答える