簡體   English   中英

如何使用 Python 使用 chrome 在所需位置下載文件?

[英]How can I download a file in the desired location with chrome using Python?

我嘗試了以下代碼來更改 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)

任何幫助,將不勝感激 !!!!

您好,這是正確的方法

代碼:

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM