python - 如何在 selenium 中使用 chrome webdriver 在 python 中下载文件?

标签 python google-chrome selenium

基于帖子herehere我正在尝试在 selenium 中使用 chrome webdriver 来下载文件。这是到目前为止的代码

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_experimental_option("profile.default_content_settings.popups", 0)
chrome_options.add_experimental_option("download.prompt_for_download", "false")
chrome_options.add_experimental_option("download.default_directory", "/tmp")

driver = webdriver.Chrome(chrome_options=chrome_options)

但这会导致以下错误:

WebDriverException: Message: unknown error: cannot parse capability: chromeOptions
from unknown error: unrecognized chrome option: download.default_directory
  (Driver info: chromedriver=2.24.417424 (c5c5ea873213ee72e3d0929b47482681555340c3),platform=Linux 4.10.0-37-generic x86_64)

那么如何解决这个问题呢?我必须使用这种“能力”吗?如果是这样,具体如何?

最佳答案

试试这个。在windows上执行

( How to control the download of files with Selenium Python bindings in Chrome )

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_experimental_option("prefs", {
  "download.default_directory": r"C:\Users\xxx\downloads\Test",
  "download.prompt_for_download": False,
  "download.directory_upgrade": True,
  "safebrowsing.enabled": True
})

关于python - 如何在 selenium 中使用 chrome webdriver 在 python 中下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46937319/

相关文章:

javascript - chrome 扩展禁用单击关闭通知按钮时的通知声音

python - 访问搜索栏并使用 selenium 进行搜索

python - 在匹配元素处划分列表的 Pythonic 和有效方法是什么?

python - 从 Python 中的列表打印?

Android chrome 始终使用 srcset 中的全尺寸图像

javascript - 为什么 Chrome V8 JavaScript 引擎将 "TG-1"到 "TG-12"识别为有效日期/时间?

python - 读取 pandas 中的 excel 文件 - 带有 NaN 标题的列

python - python 3的密码学工具

java - 当有两个或三个单选按钮时如何使用 Selenium 选择单选按钮

java - 如何使用 Maven 使用 Selenium 3.4.0 启动 FireFoxDriver?