python - 在 headless 火狐(Ubuntu)上使用 selenium 使用 python 下载文件

标签 python selenium ubuntu selenium-webdriver firefox

我试图找出一种方法让这个脚本通过 headless Firefox 下载文件。我没有看到它出现在我的下载目录中并且我没有收到任何错误,有人可以帮我弄清楚如何实际下载这个文件吗?当不在 headless 模式下完成时,此代码会定期工作。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
import requests
from selenium.webdriver.firefox.options import Options as FirefoxOptions


options = FirefoxOptions()
options.add_argument("--headless")
driver = webdriver.Firefox(options=options)
driver.get('website')
sleep(2)
driver.maximize_window()
search = driver.find_element_by_id('UserName')
search.send_keys('username')
search = driver.find_element_by_id('Password')
search.send_keys('password')
search.send_keys(Keys.RETURN)
sleep(4)
driver.get('website')
sleep(8)

try:
    element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "ej2-datetimepicker_0_input"))
            )
    element.click
    link = driver.find_element_by_id('ej2-datetimepicker_0_input')
    link.click()
   
except:
    driver.quit()
driver.implicitly_wait(3)    
date = driver.find_element_by_xpath('//*[@id="ej2-datetimepicker_0_input"]')
date.clear()
date.send_keys('7/1/2020 - 8/24/2020')


sleep(3)
WebDriverWait(driver, 8).until(EC.element_to_be_clickable((By.XPATH, "//button[@mat-button]/span[@class='mat-button-wrapper']//span[text()='Excel']"))).click()
sleep(1)
print('done')


最佳答案

我也遇到了同样的问题。就我而言,我正在下载 pdf。
您可以尝试禁用弹出下载对话框吗?
以下对我有用。

from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options


s = Service('./driver/geckodriver.exe')
download_dir = "download directory"

options = Options()
options.set_preference("pdfjs.disabled", True)
options.set_preference("browser.download.folderList", 2)
options.set_preference("browser.download.manager.useWindow", False)
options.set_preference("browser.download.dir", download_dir)
options.set_preference("browser.helperApps.neverAsk.saveToDisk", 
                       "application/pdf, application/force-download")
options.add_argument("--headless")
options.add_argument('--disable-gpu')
driver = webdriver.Firefox(service=s, options=options)

关于python - 在 headless 火狐(Ubuntu)上使用 selenium 使用 python 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64084079/

相关文章:

deployment - netbeans c++ 部署

docker - 无法准备上下文 : unable to evaluate symlinks in Dockerfile path: lstat/var/lib/snapd/void/Dockerfile: no such file or directory

python - 导入错误 : cannot import name 'cross_validation' from 'sklearn'

python - drf-yasg/drf-spectaulous - 滤波器参数描述

java - Selenium WebDriver get(url) 速度问题

javascript - Selenium 的 PhantomJS Webdriver 未在 ReactJS 中加载页面

找不到来自 firebug 的 Python Selenium Xpath

python - Numpy:如何创建类似网格的数组?

javascript - 查看未完成的请求

linux - Linux 中 applescript 的替代品是什么?它们有何不同?