将文件下载到默认位置selenium-python



我正在尝试以下行

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
opt = Options()
opt.headless = True
opt.add_experimental_option ("debuggerAddress", "localhost:9090")
opt.add_argument("download.default_directory=C:/Users/Future/Desktop")
driver = webdriver.Chrome(executable_path="D:/Webdrivers/chromedriver.exe", options=opt)
driver.switch_to.window(driver.window_handles[0])

虽然我在代码中尝试更改默认文件夹的路径,但该文件已下载,但位于下载文件夹中。

该代码设计用于在已打开的浏览器上运行。有什么想法吗?

我相信您正在寻找.add_experimental_option(...

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opt = Options()
opt.add_experimental_option("prefs", {
"download.default_directory": r"C:/Users/Future/Desktop",
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing_for_trusted_sources_enabled": False,
"safebrowsing.enabled": False
})
driver = webdriver.Chrome(executable_path=r'D:/Webdrivers/chromedriver.exe', options=opt)
url = 'https://stackoverflow.com/questions/68114651/download-file-to-default-location-selenium-python/68115019#68115019'
# Load URL
driver.get(url)

相关内容

  • 没有找到相关文章

最新更新