Chromedriver,Selenium-自动下载 [英] Chromedriver, Selenium - Automate downloads

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

问题描述

我正在将Selenium 2.43.0与Python 2.7.5一起使用.在某一时刻,测试单击一个按钮,该按钮会将表单信息发送到服务器.如果请求成功,服务器将以

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:

硒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

推荐答案

我不得不深入研究

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-自动下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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