本文介绍了Chromedriver, Selenium - 自动下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Python 2.7.5 中使用 Selenium 2.43.0.在某一时刻,测试单击将表单信息发送到服务器的按钮.如果请求成功,服务器响应

I am using Selenium 2.43.0 with Python 2.7.5. At one point, the test clicks on a button which sends form information to the server. If the request is successful, the server responds with

1) 一条成功的消息

2) 合并表单信息的 PDF

2) A PDF with the form information merged in

我不在乎测试 PDF,我的测试只是在寻找成功的消息.但是,PDF 是来自服务器的包响应的一部分,作为测试人员,我无法更改.

I don't care to test the PDF, my test is just looking for a successful message. However the PDF is part of the package response from the server that I, as the tester, cannot change.

直到最近,使用 Chromedriver 这从来都不是问题,因为 Chrome 会自动将 pdf 下载到其默认文件夹中.

Until recently, this was never an issue using Chromedriver, since Chrome would automatically download pdfs into its default folder.

然而,几天前,我的一个测试环境开始弹出一个单独的窗口,其中包含 pdf 的打印"屏幕,这使我的测试脱轨.

However, a few days ago one of my test environments started popping a separate window with a "Print" screen for the pdf, which derails my tests.

我不想要或不需要这个对话框.如何使用 chromedriver 的选项以编程方式抑制此对话框?(相当于 about:config 中 FireFox 的 pdfjs.disable 选项.

I don't want or need this dialog. How do I suppress this dialog programmatically using chromedriver's options? (Something equivalent to FireFox's pdfjs.disable option in about:config).

这是我当前绕过对话框的尝试,但它不起作用(通过不工作"不会禁用或禁止打印 pdf 对话框窗口):

Here is my current attempt to bypass the dialog, which does not work (by "not work" does not disable or suppress the print pdf dialog window):

    dc = DesiredCapabilities.CHROME
    dc['loggingPrefs'] = {'browser': 'ALL'}

    chrome_profile = webdriver.ChromeOptions()
    profile = {"download.default_directory": "C:\SeleniumTests\PDF",
               "download.prompt_for_download": False,
               "download.directory_upgrade": True}
    chrome_profile.add_experimental_option("prefs", profile)
    chrome_profile.add_argument("--disable-extensions")
    chrome_profile.add_argument("--disable-print-preview")

    self.driver = webdriver.Chrome(executable_path="C:\SeleniumTests\chromedriver.exe",
                                       chrome_options=chrome_profile,
                                       service_args=["--log-path=C:\SeleniumTests\chromedriver.log"],
                                       desired_capabilities=dc)

所有组件版本在两个测试环境中都相同:

All component versions are the same in both testing environments:

Selenium 2.43.0、Python 2.7.5、Chromedriver 2.12、Chrome(浏览器)38.0.02125.122

Selenium 2.43.0, Python 2.7.5, Chromedriver 2.12, Chrome (browser) 38.0.02125.122

推荐答案

我不得不深入研究 源代码 - 我找不到任何列出全套 Chrome 用户首选项的文档.

I had to dig into the source code on this one - I couldn't find any docs listing the full set of Chrome User Preferences.

关键是"plugins.plugins_disabled": ["Chrome PDF Viewer"]}

完整代码:

dc = DesiredCapabilities.CHROME
dc['loggingPrefs'] = {'browser': 'ALL'}

chrome_profile = webdriver.ChromeOptions()
profile = {"download.default_directory": "C:\SeleniumTests\PDF",
           "download.prompt_for_download": False,
           "download.directory_upgrade": True,
           "plugins.plugins_disabled": ["Chrome PDF Viewer"]}
chrome_profile.add_experimental_option("prefs", profile)

#Helpful command line switches
# http://peter.sh/experiments/chromium-command-line-switches/
chrome_profile.add_argument("--disable-extensions")

self.driver = webdriver.Chrome(executable_path="C:\SeleniumTests\chromedriver.exe",
                               chrome_options=chrome_profile,
                               service_args=["--log-path=C:\SeleniumTests\chromedriver.log"],
                               desired_capabilities=dc)

有趣的是,一揽子命令 chrome_profile.add_argument("--disable-plugins") 开关并没有解决这个问题.但无论如何,我更喜欢外科手术式的方法.

Interestingly the blanket command chrome_profile.add_argument("--disable-plugins") switch did not solve this problem. But I prefer the more surgical approach anyways.

这篇关于Chromedriver, Selenium - 自动下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 06:33