如何使用Chrome在Selenium中更改下载目录位置路径?

zaqlnxep  于 9个月前  发布在  Go
关注(0)|答案(3)|浏览(156)

我在Python中使用Selenium,我试图改变下载路径。但要么这样:

prefs = {"download.default_directory": "C:\\Users\\personal\\Downloads\\exports"}
options.add_experimental_option("prefs", prefs)`

字符串
或本

options.add_argument("--download.default_directory --C:\\Users\\personal\\Downloads\exports")`


都不管用
在第一种情况下,我也得到错误

from invalid argument: unrecognized chrome option: prefs


有人能帮忙吗?

nle07wnf

nle07wnf1#

options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_experimental_option('excludeSwitches', ['enable-logging'])
prefs = {"profile.default_content_settings.popups": 0,    
        "download.default_directory":r"C:\Users\xxxx\xxxx\ccc\xxxx\xx\xx", ### Set the path accordingly
        "download.prompt_for_download": False, ## change the downpath accordingly
        "download.directory_upgrade": True}
options.add_experimental_option("prefs", prefs)
driver = Chrome(service=Service(PATH), options=options)

字符串

2eafrhcq

2eafrhcq2#

要更改下载目录/路径,您可以使用以下代码块:
selenium4兼容代码

from selenium.webdriver.chrome.options import Options

options = Options()
options.add_experimental_option("prefs", {
  "download.default_directory": r"C:\Data_Files\output_files"
  })
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)

字符串

参考资料

您可以在以下内容中找到一些相关的详细讨论:

xiozqbni

xiozqbni3#

在互联网上尝试了无限的解决方案后,下面是我在Python Selenium Chrome中设置下载路径的方法。

from selenium.webdriver import Chrome, ChromeOptions

prefs = {
    "download.default_directory": "/Users/your_user/Desktop",
    "download.directory_upgrade": True,
    "download.prompt_for_download": False,
}

chromeOptions = ChromeOptions()
chromeOptions.add_experimental_option("prefs", prefs)
driver = Chrome(options=chromeOptions)

字符串

相关问题