Chromedriver,Selenium - 自动下载

时间:2014-11-12 18:36:12

标签: python python-2.7 pdf selenium selenium-chromedriver

我正在使用Selenium 2.43.0和Python 2.7.5。有一次,测试点击了一个按钮,该按钮将表单信息发送到服务器。如果请求成功,服务器将以

响应

1)成功的消息

2)包含表格信息的PDF

我不在乎测试PDF,我的测试只是寻找成功的消息。然而,PDF是来自服务器的包响应的一部分,作为测试者,我不能改变。

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

然而,几天前,我的一个测试环境开始弹出一个单独的窗口,其中包含" Print" pdf的屏幕,这会破坏我的测试。

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

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

    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)

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

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

1 个答案:

答案 0 :(得分:12)

我不得不深入研究这个问题的source code - 我无法找到任何列出整套Chrome用户首选项的文档。

关键是"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")切换并没有解决这个问题。但无论如何我更喜欢手术方法。

相关问题