【问题标题】:How to specify the folder when downloading a file with slelenium python使用selenium python下载文件时如何指定文件夹
【发布时间】:2022-01-06 10:55:56
【问题描述】:

这是我的代码:

from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait     
from selenium.webdriver.common.by import By     
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

PATH = "driver\chromedriver.exe"

options = webdriver.ChromeOptions() 

p = {"download.default_directory": "C:\\Users", "safebrowsing.enabled":"false"}
options.add_experimental_option("prefs", p)

driver = webdriver.Chrome(options=options, executable_path=PATH)

url = 'https://www.mergermarket.com/homepage'

driver.get(url)

download = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="__next"]/div/div/div[1]/div/div[2]/div[1]/div/div/div[2]/button[1]')))
download.click()

我做了一些研究,但几乎每个人都建议使用 download.default_directoryChromeOptions。我确实喜欢你在上面看到的,但它没有用。好吧,代码可以工作,但我的文件是在下载中下载的,而不是我在代码中指定的路径。

【问题讨论】:

  • 您需要管理员权限才能更改用户文件夹。尝试不同的目录。
  • 哦,我明白了,让我们试试吧。
  • 但是如果我想对用户文件夹进行更改,可以吗?
  • 请不要在问题中编辑解决方案公告。接受(即单击旁边的“勾选”)现有答案之一,如果有的话。如果现有答案尚未涵盖您的解决方案,您还可以创建自己的答案,甚至接受它。比较stackoverflow.com/help/self-answer

标签: python python-3.x selenium path


【解决方案1】:

您可以使用os或python中的shutill库将自动下载的文件复制到作者路径中

【讨论】:

  • 它不会下载所有文件?因为里面有很多
  • 而且,下载时提供的名称是随机的,所以很棘手
  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2018-04-11
  • 2023-03-12
  • 1970-01-01
  • 2020-08-03
  • 2014-10-04
  • 2017-08-26
  • 1970-01-01
相关资源
最近更新 更多