如何处理弹出 window 对话框以在 Linux (Ubuntu) 系统上使用 python selenium 中的 firefox 配置文件自动下载文件
How to handle pop up window dialog to download file automatically with firefox profile in python selenium on Linux (Ubuntu) system
我试图通过在我的 python selenium 代码中的 firefox 配置文件中设置首选项,从系统文件下载对话框自动下载文件,但我的代码不起作用。
浏览器:Firefox 72.0
硒版本:3.14
OS : linux Ubuntu
要下载的文件类型:*.enc(加密文件类型)
linux 中的 firefox 路径:/usr/bin/firefox
代码:
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", "/home/user/Downloads/tests")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-uuencoded,application/octet-stream")
self.driver = webdriver.Firefox(firefox_profile=profile)
我在使用 Firefox 72.0 时遇到了同样的问题,但使用的是 pdf 文件。这是代码:
fp = webdriver.FirefoxProfile()
fp.set_preference("pdfjs.disabled", True)
fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.dir", "/path")
fp.set_preference("browser.download.downloadDir", "/path")
fp.set_preference("browser.download.defaultFolder", "/path")
fp.set_preference("plugin.disable_full_page_plugin_for_types", "application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf, application/vnd.cups-pdf")
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf, application/vnd.cups-pdf")
fp.set_preference("browser.helperApps.neverAsk.openFile", "application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf, application/vnd.cups-pdf")
driver = webdriver.Firefox(firefox_profile=fp)
我已经尝试了所有可能的首选项,但它总是触发下载警报。
嗨@Sum 我已经解决了,我的问题是不同的内容类型。
使用此示例解决您的问题,并了解您的内容类型:
在我的例子中,Content-Type 是 "application/force-download" 而不是 "application/pdf"
profile.set_preference("pdfjs.disabled", True)
profile.set_preference("browser.download.folderList",2)
profile.set_preference("browser.download.manager.useWindow", False)
profile.set_preference("browser.download.dir", "<path>")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf, application/force-download")
这些设置对我有用。希望对你有帮助。
.enc
的正确 MIME 类型是 "text/x-uuencoded"
在代码中更新如下,它正在运行:
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/x-uuencoded")
试试这个会很有效......
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import time
import pyautogui
try :
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.maximize_window()
driver.get("https://www.citysdk.eu/wp-content/uploads/2013/09/DELIVERABLE_WP4_TA_SRS_0.21.pdf")
WebDriverWait(driver, 10).until(lambda d: d.execute_script('return document.readyState') == 'complete')
# Click the OK button and close
time.sleep(5)
webelem = driver.find_element_by_id('download')
webelem.click()
time.sleep(5)
print('press enter')
pyautogui.press('enter')
except Exception as err:
print('ERROR: %sn' % str(err))
driver.quit()
我试图通过在我的 python selenium 代码中的 firefox 配置文件中设置首选项,从系统文件下载对话框自动下载文件,但我的代码不起作用。
浏览器:Firefox 72.0 硒版本:3.14 OS : linux Ubuntu 要下载的文件类型:*.enc(加密文件类型) linux 中的 firefox 路径:/usr/bin/firefox
代码:
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", "/home/user/Downloads/tests")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-uuencoded,application/octet-stream")
self.driver = webdriver.Firefox(firefox_profile=profile)
我在使用 Firefox 72.0 时遇到了同样的问题,但使用的是 pdf 文件。这是代码:
fp = webdriver.FirefoxProfile()
fp.set_preference("pdfjs.disabled", True)
fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.dir", "/path")
fp.set_preference("browser.download.downloadDir", "/path")
fp.set_preference("browser.download.defaultFolder", "/path")
fp.set_preference("plugin.disable_full_page_plugin_for_types", "application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf, application/vnd.cups-pdf")
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf, application/vnd.cups-pdf")
fp.set_preference("browser.helperApps.neverAsk.openFile", "application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf, application/vnd.cups-pdf")
driver = webdriver.Firefox(firefox_profile=fp)
我已经尝试了所有可能的首选项,但它总是触发下载警报。
嗨@Sum 我已经解决了,我的问题是不同的内容类型。
使用此示例解决您的问题,并了解您的内容类型:
在我的例子中,Content-Type 是 "application/force-download" 而不是 "application/pdf"
profile.set_preference("pdfjs.disabled", True)
profile.set_preference("browser.download.folderList",2)
profile.set_preference("browser.download.manager.useWindow", False)
profile.set_preference("browser.download.dir", "<path>")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf, application/force-download")
这些设置对我有用。希望对你有帮助。
.enc
的正确 MIME 类型是 "text/x-uuencoded"
在代码中更新如下,它正在运行:
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/x-uuencoded")
试试这个会很有效......
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import time
import pyautogui
try :
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.maximize_window()
driver.get("https://www.citysdk.eu/wp-content/uploads/2013/09/DELIVERABLE_WP4_TA_SRS_0.21.pdf")
WebDriverWait(driver, 10).until(lambda d: d.execute_script('return document.readyState') == 'complete')
# Click the OK button and close
time.sleep(5)
webelem = driver.find_element_by_id('download')
webelem.click()
time.sleep(5)
print('press enter')
pyautogui.press('enter')
except Exception as err:
print('ERROR: %sn' % str(err))
driver.quit()