[英]Download file in Headless Chrome, (python)
I tried everything to download a file in headless chrome but nothing works, I'm using Chrome version 86.0.4240.75 while ChromeDriver version: 86.0.4240.22, I've already tried any solution and none of them worked我尝试了所有方法来下载无头 chrome 中的文件,但没有任何效果,我使用的是 Chrome 版本 86.0.4240.75,而 ChromeDriver 版本:86.0.4240.22,我已经尝试过任何解决方案,但都没有奏效
download_dir = "/tmp/"
options.add_argument("--start--minimized")
options.add_experimental_option("prefs", {
"download.default_directory": download_dir,
"download.prompt_for_download": False,
})
browser.get(www.download.com)
browser.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': download_dir}}
command_result = browser.execute("send_command", params)
When I try to specify the download directory as well without headless mode it gives me a common download chrome error当我尝试在没有无头模式的情况下也指定下载目录时,它给了我一个常见的下载 chrome 错误
My use case is a little different - I'm navigating to a page and submitting a form - but I am getting working downloads with this code:我的用例有点不同 - 我正在导航到一个页面并提交一个表单 - 但我正在使用以下代码进行下载:
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_prefs = {"download.default_directory": "/root/Downloads"}
chrome_options.experimental_options["prefs"] = chrome_prefs
chrome_prefs["profile.default_content_settings"] = {"images": 2}
driver = webdriver.Chrome(options=chrome_options)
driver.get('https://...redacted...')
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//a[contains(text(),'ContractOp')]")))
submit_button = driver.find_element_by_xpath("//button[contains(.,'Submit')]")
submit_button.click()
# wait for download to finish
Hope this is helpful for you.希望这对你有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.