使用Selenium Chrome驱动程序自动“另存为PDF"时缺少元素 [英] Missing elements when using selenium chrome driver to automatically 'Save as PDF'

查看:545
本文介绍了使用Selenium Chrome驱动程序自动“另存为PDF"时缺少元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动保存使用pdftohtmlEX创建的PDF文件( https://github.com/coolwanglu/pdf2htmlEX )使用硒(chrome)网络驱动程序.

I am trying to automatically save a PDF file created with pdftohtmlEX (https://github.com/coolwanglu/pdf2htmlEX) using the selenium (chrome) webdriver.

除了图形的标题外,几乎可以使用,有时甚至丢失了部分图形.

It almost works except captions of figures and sometimes even part of the figures are missing.

手动保存:

使用硒自动保存chrome webdriver:

Automatically saved using selenium & chrome webdriver:

这是我的代码(您需要Chrome网络驱动程序( http://chromedriver. Chrome.org/downloads )与此脚本位于同一文件夹中):

Here is my code (you need the chromium webdriver (http://chromedriver.chromium.org/downloads) in the same folder as this script):

import json
from selenium import webdriver

# print settings: save as pdf, 'letter' formatting
appState = """{
    "recentDestinations": [
        {
            "id": "Save as PDF",
            "origin": "local"
        }
    ],
    "mediaSize": {
        "height_microns": 279400,
        "name": "NA_LETTER",
        "width_microns": 215900,
        "custom_display_name": "Letter"
    },
    "selectedDestinationId": "Save as PDF",
    "version": 2
}"""

appState = json.loads(appState)
profile = {"printing.print_preview_sticky_settings.appState": json.dumps(appState)}
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('prefs', profile)
# Enable automatically pressing the print button in print preview
# https://peter.sh/experiments/chromium-command-line-switches/
chrome_options.add_argument('--kiosk-printing')

driver = webdriver.Chrome('./chromedriver', options=chrome_options)
driver.get('http://www.deeplearningbook.org/contents/intro.html')
driver.execute_script('window.print();')
driver.quit()

有时,当我手动打印时也会发生这种情况.但是,如果我随后更改任何打印选项,则无论我进一步启用/禁用了什么选项,预览都会重新加载,并且图像标题会再次出现并停留在该位置.

Sometimes when I manually print this happens, too. But if I then change any of the printing options, the preview reloads and the image captions are there again and stay there no matter what options I further enable/disable.

  • different Chrome webdriver versions (71, 72, 73) from this site: http://chromedriver.chromium.org/downloads
  • enable background graphics by adding '"isCssBackgroundEnabled": true' to the appState

推荐答案

因此,通过四处寻找,我偶然遇到了解决方案.我真的不明白为什么,但是启用"PrintBrowser模式"(启用PrintBrowser模式,所有内容都呈现为打印状态.")解决了该问题.这可能与CSS正确加载有关.

So, through fiddeling around, I came by the solution by accident. I don't really understand why, but enabling the 'PrintBrowser mode' ("Enables PrintBrowser mode, in which everything renders as though printed.") solves the issue. This may or may have to do with CSS loading properly.

我只需要添加chrome_options.add_argument('--enable-print-browser')并且所有元素都在那里!

I just need to add chrome_options.add_argument('--enable-print-browser') and all elements are there!

这篇关于使用Selenium Chrome驱动程序自动“另存为PDF"时缺少元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆