python - 如何使用 Python 在 chrome 中将文件下载到所需位置?

标签 python selenium google-chrome download

我尝试了以下代码来更改chrome的默认下载位置。但该文件仍然是在“下载”处下载的。

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

ChromeOptions=Options()
ChromeOptions.add_experimental_option("prefs", {"download.default_directory":"C:\\Users\Elite\Desktop"})
driver=webdriver.Chrome(executable_path='C:\Webdrivers\chromedriver.exe',chrome_options=ChromeOptions)
driver.get("url/website")
driver.find_element_by_xpath('xpath of download file').click()

我也尝试过这个:

options = webdriver.ChromeOptions()
options.add_argument('download.default_directory=C:/Users/Elite/Desktop')
driver=webdriver.Chrome(options=options)

任何帮助将不胜感激!!!!

最佳答案

嗨,这是正确的方法

Code:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()

options.add_experimental_option("prefs", {
  "download.default_directory": r"C:\Users\Elite\Desktop",
  "download.directory_upgrade":True,
  "safebrowsing.enabled":True,
  "download.prompt_for_download":False,
})

driver=webdriver.Chrome(executable_path="C:\Webdrivers\chromedriver.exe",options=options)

driver.get("url/website")
driver.find_element_by_xpath('xpath of download file').click()

关于python - 如何使用 Python 在 chrome 中将文件下载到所需位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63534241/

相关文章:

python - 用于 python 的轻量级 DBAL

python - 导入错误 : No module named 'utils'

python - pySerial 非常奇怪的行为...代码在 shell 中执行时有效,但在脚本中无效

python - 包裹在 SIP 中的 C++ 类在 Python 中修补

html - 使用 Selenium 获取 HTML 注释标签的内容

javascript - 将本地主机时间设置为 Protractor

c# - 需要帮助理解程序集引用

javascript - 对于 Chrome 上动态加载的 iframe,contentWindow 未定义

java - chromedriver 中未加载 GWT 插件

javascript - 是否可以在 Chrome for Android 中以编程方式显示 URL 栏?