Chrome/Firefox在新选项卡中打开PDF,但不以无头模式保存(Selenium+;Python)

Chrome/Firefox在新选项卡中打开PDF,但不以无头模式保存(Selenium+;Python),python,selenium,google-chrome,firefox,selenium-webdriver,Python,Selenium,Google Chrome,Firefox,Selenium Webdriver,在执行测试时,我对无头chrome有一个问题:当单击按钮时,一个PDF文件会在一个新选项卡中打开。如果我在非headless模式下运行测试,一切正常。但当尝试在headless中执行相同操作时,文件不会下载 options = ChromeOptions() options.add_argument('--no-sandbox') options.add_argument('--kiosk-printing') option

在执行测试时,我对无头chrome有一个问题:当单击按钮时,一个PDF文件会在一个新选项卡中打开。如果我在非headless模式下运行测试,一切正常。但当尝试在headless中执行相同操作时,文件不会下载

options = ChromeOptions()
            options.add_argument('--no-sandbox')
            options.add_argument('--kiosk-printing')
            options.add_argument('--test-type')
            options.add_argument('--disable-infobars')
            options.add_argument('disable-gpu')
            options.add_argument('--verbose')
            options.add_argument('--disable-extensions')
            options.add_argument('--ignore-certificate-errors')
            options.add_experimental_option("prefs", {
                "profile.default_content_settings.popups": 0,
                "download.default_directory": dwnld_path,
                "download.prompt_for_download": False,
                "download.directory_upgrade": True,
                "safebrowsing.enabled": False,
                "plugins.always_open_pdf_externally": True,
                "plugins.plugins_disabled": ["Chrome PDF Viewer"]
            })
我还发现:

wd.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')

params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': dwnld_path}}
        command_result = wd.execute("send_command", params)

但是,只有当我收到以无头模式下载的请求时,它才有帮助,而不是在浏览器中打开文件时。

尝试保存PDF文件的url并使用请求库下载,我认为它会起作用

像这样:

import urllib3
import PyPDF2
import certifi
import io

http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where())
pdf_url = "http:\\XXXXXX.pdf"
r3 = http.request('GET', pdf_url)
with io.BytesIO(r3.data) as open_pdf_file:
      read_pdf = PyPDF2.PdfFileReader(open_pdf_file)
      num_pages = read_pdf.getNumPages()

然后我们需要代码的第二部分,您需要以类似的方式保存pdf

尝试保存pdf文件的url并使用请求库下载,我认为它会起作用

像这样:

import urllib3
import PyPDF2
import certifi
import io

http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where())
pdf_url = "http:\\XXXXXX.pdf"
r3 = http.request('GET', pdf_url)
with io.BytesIO(r3.data) as open_pdf_file:
      read_pdf = PyPDF2.PdfFileReader(open_pdf_file)
      num_pages = read_pdf.getNumPages()

然后我们需要代码的第二部分,您需要以类似的方式保存pdf

不幸的是,JS没有显示=\File,当我尝试使用wget或urllib从URL获取文件或请求时,它会下载带有JS的HTML源代码示例:我的URL是页面源代码包含blablablablasome JS代码,如果我按ctrl+s,它会下载文件_name.pdf。如果我使用wget,urllib,请求它下载源代码。所以,我需要一种ctrl+s模拟。。。我宁愿不使用动作链。。。它太“ducktapely”(=Nono,没有html文件,只需从html中获取pdf\u url,请查看我上面编辑的评论。in
read\u pdf=PyPDF2.PdfFileReader(打开\u pdf\u文件)
我得到一个错误:
PyPDF2.utils.pdfReader:EOF标记未找到
不幸的是,JS没有显示=\File,当我尝试使用wget或urllib从URL获取文件或请求时,它会下载带有JS的HTML源代码示例:我的URL是页面源代码,如果我按ctrl+s,它会下载包含BlablablablablablablablaBlaBlaBlasSome JS代码如果我使用wget,urllib,请求它下载源代码。因此,我需要一种ctrl+s模拟…我不想使用ActionChains…它太“ducktapely”(=Nono,没有html文件,只需从html获取pdf url,请查看我上面编辑的评论。
read\u pdf=PyPDF2.PdfFileReader(open\u pdf\u file)
我收到一个错误:
PyPDF2.utils.pdfReader错误:找不到EOF标记