Chromedriver,Selenium - 自动化下载

3
我正在使用Python 2.7.5和Selenium 2.43.0。在测试中,单击一个按钮将表单信息发送到服务器。如果请求成功,服务器会响应:
1)成功消息
2)合并了表单信息的PDF
我不需要测试PDF,我的测试只关注成功消息。但是,PDF是作为来自服务器的包响应的一部分,作为测试者,我无法更改。
直到最近,使用Chromedriver从未出现过问题,因为Chrome会自动下载pdf到其默认文件夹中。
然而,几天前,我的一个测试环境开始弹出一个“打印”屏幕的单独窗口,这使我的测试失败了。
我不想要或不需要这个对话框。如何使用chromedriver的选项以编程方式禁止此对话框?(类似于Firefox中about:config的pdfjs.disable选项)
以下是我当前尝试绕过对话框的方法,但它不起作用(“不起作用”表示无法禁用或抑制打印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。


有些人成功地将其打印为PDF:https://dev59.com/questions/uabja4cB1Zd3GeqPe1id#48798425 - Nemo
1个回答

13

我必须深入研究源代码 - 我找不到任何列出完整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") 不能解决这个问题。但无论如何,我更喜欢更精细的方法。


文档 https://sites.google.com/a/chromium.org/chromedriver/capabilities https://dev59.com/Mabja4cB1Zd3GeqPgXKh - Nemo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接