我需要从一个站点下载文件,例如使用thinkbroadband网站。已经找到了关于此的多个帖子,但是所有解决方案似乎都不起作用。我正在使用Chrome v73。从我发现的一种无效的解决方案中使用此方法
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("prefs", {
"download.default_directory": "/path/to/download/dir",
"download.prompt_for_download": False,
})
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
driver = webdriver.Chrome(executable_path="./chromedriver", chrome_options=chrome_options)
driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': "/path/to/download/dir"}}
command_result = driver.execute("send_command", params)
driver.get('https://www.thinkbroadband.com/download')
driver.find_element_by_xpath('//*[@id="main-col"]/div/div/div[7]/p[2]/a[1]').click()
没有错误,但是脚本存在时也没有文件