Selenium Python - 下载文件时出现问题,首选项不起作用
Selenium Python - problem with downloading file, preferences are not working
使用 selenium 时,我无法设置自动下载 CSV 文件的配置文件。
除其他外,我尝试了以下问题中提供的解决方案:
How do I automatically download files from a pop up dialog using selenium-python
https://sqa.stackexchange.com/questions/2197/how-to-download-a-file-using-seleniums-webdriver
在我的“常规 firefox”上,我可以设置自动下载规则,它有效,但是当 geckodriver 运行 firefox 时,这个 rule/profile 不适用。
我有管理员权限。当我读到另一个问题时,我在 firefox 中的橙色条是通过 webdriver 控制它的结果。
这是我的代码示例:
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.panel.shown", False)
profile.set_preference("browser.helperApps.neverAsk.openFile","text/csv,application/vnd.ms-excel")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "CSV File,text/csv,application/vnd.ms-excel")
profile.set_preference("browser.download.folderList", 2);
profile.set_preference("browser.download.dir", "F:\Delete")
driver=webdriver.Firefox(firefox_profile=profile)
1.你们能帮我理解我错过了什么吗?
2。如何自动下载 CSV 文件?
我一直在使用以下逻辑并在 Java 为我工作,您可以轻松地将其移植到 python。
FirefoxProfile profile = new FirefoxProfile();
// set the download folder directory
profile.setPreference("browser.download.dir", this.getDownloadFolderPath());
// the last folder specified for a download
profile.setPreference("browser.download.folderList", 2);
// hide Download Manager window when a download begins
profile.setPreference("browser.download.manager.showWhenStarting", false);
/**
This is the most important setting that will make sure the pdf is downloaded
without any prompt
*/
profile.setPreference("pdfjs.disabled", true);
profile.setPreference("pref.downloads.disable_button.edit_actions", false);
profile.setPreference("media.navigator.permission.disabled", true);
// A comma-separated list of MIME types to save to disk without asking what to
// use to open the file.
profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/pdf,application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/zip,text/csv,text/plain,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;octet/stream");
// A comma-separated list of MIME types to open directly without asking for
// confirmation.
profile.setPreference("browser.helperApps.neverAsk.openFile",
"application/pdf,application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/zip,text/csv,text/plain,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;octet/stream");
// Do not ask what to do with an unknown MIME type
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
// Leave the window in the background when starting a download (Default Setting
// is false)
profile.setPreference("browser.download.manager.focusWhenStarting", false);
// popup window at bottom right corner of the screen will not appear once all
// downloads are finished.
profile.setPreference("browser.download.manager.showAlertOnComplete", true);
// Close the Download Manager when all downloads are complete
profile.setPreference("browser.download.manager.closeWhenDone", true);
FirefoxOptions options = new FirefoxOptions();
options.setProfile(profile);
确保在创建驱动程序实例时使用 options
。
如果您仍然面临这个问题,请告诉我。
我在一些 CSV
上测试了你的代码,我不得不使用 application/octet-stream
因为有时服务器可能会发送不同类型的文件。
我记得 application/octet-stream
是网页上强制下载的流行方法 - 特别是对于 PDF
- 因为某些用户可能在浏览器中设置了内置显示 PDF
查看器而不是下载。
带有示例的最小工作代码CSV
from selenium import webdriver
url = 'https://data.europa.eu/euodp/pl/data/dataset/covid-19-coronavirus-data'
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.panel.shown", False)
profile.set_preference("browser.helperApps.neverAsk.openFile","text/csv,application/vnd.ms-excel,application/octet-stream")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "CSV File,text/csv,application/vnd.ms-excel")
profile.set_preference("browser.download.folderList", 2);
profile.set_preference("browser.download.dir", "/home/furas/")
driver = webdriver.Firefox(firefox_profile=profile)
driver.get(url)
item = driver.find_element_by_xpath('//div[@id="dataset-resources"]//li[2]//a')
print('text:', item.text)
print('href:', item.get_attribute('href'))
item.click()
编辑: 对于其他用户:它还需要 binary/octet-stream
使用 selenium 时,我无法设置自动下载 CSV 文件的配置文件。
除其他外,我尝试了以下问题中提供的解决方案:
How do I automatically download files from a pop up dialog using selenium-python
https://sqa.stackexchange.com/questions/2197/how-to-download-a-file-using-seleniums-webdriver
在我的“常规 firefox”上,我可以设置自动下载规则,它有效,但是当 geckodriver 运行 firefox 时,这个 rule/profile 不适用。
我有管理员权限。当我读到另一个问题时,我在 firefox 中的橙色条是通过 webdriver 控制它的结果。
这是我的代码示例:
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.panel.shown", False)
profile.set_preference("browser.helperApps.neverAsk.openFile","text/csv,application/vnd.ms-excel")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "CSV File,text/csv,application/vnd.ms-excel")
profile.set_preference("browser.download.folderList", 2);
profile.set_preference("browser.download.dir", "F:\Delete")
driver=webdriver.Firefox(firefox_profile=profile)
1.你们能帮我理解我错过了什么吗?
2。如何自动下载 CSV 文件?
我一直在使用以下逻辑并在 Java 为我工作,您可以轻松地将其移植到 python。
FirefoxProfile profile = new FirefoxProfile();
// set the download folder directory
profile.setPreference("browser.download.dir", this.getDownloadFolderPath());
// the last folder specified for a download
profile.setPreference("browser.download.folderList", 2);
// hide Download Manager window when a download begins
profile.setPreference("browser.download.manager.showWhenStarting", false);
/**
This is the most important setting that will make sure the pdf is downloaded
without any prompt
*/
profile.setPreference("pdfjs.disabled", true);
profile.setPreference("pref.downloads.disable_button.edit_actions", false);
profile.setPreference("media.navigator.permission.disabled", true);
// A comma-separated list of MIME types to save to disk without asking what to
// use to open the file.
profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/pdf,application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/zip,text/csv,text/plain,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;octet/stream");
// A comma-separated list of MIME types to open directly without asking for
// confirmation.
profile.setPreference("browser.helperApps.neverAsk.openFile",
"application/pdf,application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/zip,text/csv,text/plain,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;octet/stream");
// Do not ask what to do with an unknown MIME type
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
// Leave the window in the background when starting a download (Default Setting
// is false)
profile.setPreference("browser.download.manager.focusWhenStarting", false);
// popup window at bottom right corner of the screen will not appear once all
// downloads are finished.
profile.setPreference("browser.download.manager.showAlertOnComplete", true);
// Close the Download Manager when all downloads are complete
profile.setPreference("browser.download.manager.closeWhenDone", true);
FirefoxOptions options = new FirefoxOptions();
options.setProfile(profile);
确保在创建驱动程序实例时使用 options
。
如果您仍然面临这个问题,请告诉我。
我在一些 CSV
上测试了你的代码,我不得不使用 application/octet-stream
因为有时服务器可能会发送不同类型的文件。
我记得 application/octet-stream
是网页上强制下载的流行方法 - 特别是对于 PDF
- 因为某些用户可能在浏览器中设置了内置显示 PDF
查看器而不是下载。
带有示例的最小工作代码CSV
from selenium import webdriver
url = 'https://data.europa.eu/euodp/pl/data/dataset/covid-19-coronavirus-data'
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.panel.shown", False)
profile.set_preference("browser.helperApps.neverAsk.openFile","text/csv,application/vnd.ms-excel,application/octet-stream")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "CSV File,text/csv,application/vnd.ms-excel")
profile.set_preference("browser.download.folderList", 2);
profile.set_preference("browser.download.dir", "/home/furas/")
driver = webdriver.Firefox(firefox_profile=profile)
driver.get(url)
item = driver.find_element_by_xpath('//div[@id="dataset-resources"]//li[2]//a')
print('text:', item.text)
print('href:', item.get_attribute('href'))
item.click()
编辑: 对于其他用户:它还需要 binary/octet-stream