【问题标题】:Chromedriver, Selenium - Automate downloadsChromedriver, Selenium - 自动下载
【发布时间】:2015-01-09 17:22:50
【问题描述】:

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

1) 一条成功的消息

2) 合并表单信息的PDF

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

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

但是,几天前,我的一个测试环境开始弹出一个单独的窗口,其中包含 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

【问题讨论】:

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


【解决方案1】:

我不得不深入研究这个 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") 开关并没有解决这个问题。但无论如何我更喜欢手术方法。

猜你喜欢
  • 2021-11-16
  • 2020-09-12
  • 2018-09-30
  • 2017-11-27
  • 2016-02-17
  • 1970-01-01
  • 2018-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多