如何使用 selenium-python 从弹出对话框中自动下载文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24852709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I automatically download files from a pop up dialog using selenium-python
提问by bernie2436
I am trying to automatically download files from a pop up dialog using selenium-python.
我正在尝试使用 selenium-python 从弹出对话框中自动下载文件。
The firefox popups look like this
Firefox 弹出窗口看起来像这样
I want to simulate clicking "OK"
我想模拟点击“确定”
I found this answer How do I trap a popup in Selenium 2 pythonwhich sent me to the docs https://selenium-python.readthedocs.org/en/latest/navigating.html?highlight=popup#popup-dialogs
我找到了这个答案How do I trap a popup in Selenium 2 python将我发送到文档https://selenium-python.readthedocs.org/en/latest/navigating.html?highlight=popup#popup-dialogs
I've tried this
我试过这个
alert = driver.switch_to_alert()
#alert.send_keys(Keys.RETURN) #No alert is present
and this
和这个
alert = driver.switch_to_alert()
alert.accept() #no alert is present
If I run pprint.pprint(driver.window_handles)
it prints only a single GUID -- showing that only one window is present.
如果我运行pprint.pprint(driver.window_handles)
它,它只打印一个 GUID——显示只有一个窗口存在。
So if no alert is present and there is only one window -- how do I download these files?
因此,如果不存在警报并且只有一个窗口——我该如何下载这些文件?
回答by Amey
You have two options :
你有两个选择:
1) Create a custom firefox profile with settings where the download location is pre-decided and firefox does not ask for confirmation to download. Just googled and found a blogthat explains how to do it
1) 使用预先确定下载位置的设置创建自定义 Firefox 配置文件,并且 Firefox 不要求确认下载。 刚刚用谷歌搜索并找到了一个解释如何做的博客
2) Use sikulito automate clicks on the download dialog box. Blogexplaining- How to use Sikuli
2)使用sikuli自动点击下载对话框。 博客讲解-如何使用Sikuli
P.S. - Not read the blogs, but I am sure they will give u a clue.
PS - 不阅读博客,但我相信他们会给你线索。
回答by Sachit Dahal
FirefoxProfile fxProfile = new FirefoxProfile();
fxProfile.SetPreference("browser.download.panel.shown", false);
fxProfile.SetPreference("browser.helperApps.neverAsk.openFile", "text/csv,application/vnd.ms-excel");
fxProfile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv,application/vnd.ms-excel");
fxProfile.SetPreference("browser.download.folderList", 2);
fxProfile.SetPreference("browser.download.dir", "c:\mydownloads");
IwebDriver driver = new FirefoxDriver(fxProfile);
回答by SYK
Based on Amey's answer 1) and of course Yi Zeng's blog (in ruby)quoting Selenium itself doesn't interact with system-level dialogs like this as well as the documentation, here is the python snippet to resolve the issue
基于 Amey 的回答 1) 当然,Yi Zeng 的博客(在 ruby 中)引用 Selenium 本身不会与这样的系统级对话框以及文档交互,这里是解决问题的 python 代码段
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
profile = FirefoxProfile()
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", 'application/pdf')
driver = webdriver.Firefox(firefox_profile=profile)
driver.get(target_url)
#specific to target_url
driver.find_element_by_css_selector('a[title="Click to Download"]').click()
回答by Max
In python, but this will work in Java as well because the firefox preferences are a javascript:
在python中,但这也适用于Java,因为firefox首选项是一个javascript:
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", "text/csv,application/vnd.ms-excel")
profile.set_preference("browser.download.folderList", 2);
profile.set_preference("browser.download.dir", "c:\firefox_downloads\")
browser = webdriver.WebDriver(firefox_profile=profile)
this works for CSV files, modify it for whatever filetype you are downloading.
这适用于 CSV 文件,请针对您正在下载的任何文件类型修改它。
回答by shish
Most browsers (in mine case Firefox) select the OK button by default. So I managed to solve this by using the following code. It basically presses enter for you and the file is downloaded.
大多数浏览器(在我的情况下是 Firefox)默认选择 OK 按钮。所以我设法通过使用以下代码解决了这个问题。它基本上为您按下回车键并下载文件。
Robot robot = new Robot();
// A short pause, just to be sure that OK is selected
Thread.sleep(3000);
robot.keyPress(KeyEvent.VK_ENTER);
回答by Yang
With my using and test in my Selenium UI automation test, configuring the Firefox Profile is more stable than Robot Class. E.g. Disable popping up the System non-webpage Download/Save Dialog.
通过我在 Selenium UI 自动化测试中的使用和测试,配置 Firefox Profile 比 Robot Class 更稳定。例如禁用弹出系统非网页下载/保存对话框。
FirefoxProfile prof = new FirefoxProfile();
ffprofile.setPreference("browser.download.panel.shown", false);
ffprofile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/zip");
//ffprofile.setPreference("browser.download.folderList", 1); // Default to /home/user/Downloads in Linux.
ffprofile.setPreference("browser.download.folderList", 2);
ffprofile.setPreference("browser.download.dir", "/tmp");
回答by Seetaram Hegde
public class DemoFileDownload{
公共类演示文件下载{
FirefoxProfile prof = new FirefoxProfile(); prof.setpreference("browser.helperApps.neverAsk.SaveToDisk", "mimetype_of_file"); prof.setpreference("browser.download.folderlist",int_value); prof.setpreference("browser.download.dir,"folder_path"); // if the above int_value is 2 //int_value can be of below values: // 1 - downloads folder // 0 - desktop // 2 - custom folder } get the mime type from below website: www.sitepoint.com/mimetypes-complete-list/
FirefoxProfile prof = new FirefoxProfile(); prof.setpreference("browser.helperApps.neverAsk.SaveToDisk", "mimetype_of_file"); prof.setpreference("browser.download.folderlist",int_value); prof.setpreference("browser.download.dir,"folder_path"); // 如果上面的 int_value 是 2 //int_value 可以是以下值: // 1 - 下载文件夹 // 0 - 桌面 // 2 - 自定义文件夹从以下网站获取 mime 类型:www.sitepoint.com/mimetypes-complete-list/
for chrome browser, use chromeoptions instead of firefoxprofile
对于 chrome 浏览器,使用 chromeoptions 而不是 firefoxprofile
回答by Matt Camp
I spent some time today trying to figure this out (again... had the solution at home but couldn't get to it...) and during that I found this... None of the solutions helped me so I thought I'd offer up what I did to solve this problem.
我今天花了一些时间试图解决这个问题(再次......在家里有解决方案但无法解决......)并且在此期间我发现了这个......没有任何解决方案帮助我所以我认为我会提供我为解决这个问题所做的工作。
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
dl_path = "/tmp/"
profile = FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", false)
profile.set_preference("browser.download.dir", dl_path)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk",
"text/plain,text/x-csv,text/csv,application/vnd.ms-excel,application/csv,application/x-csv,text/csv,text/comma-separated-values,text/x-comma-separated-values,text/tab-separated-values,application/pdf")
回答by Zei
I've discovered a useful solution that hopefully will help somebody out there.
我发现了一个有用的解决方案,希望能帮助那里的人。
You can skip the prompt altogether with firefox if you're starting the download with a click by holding the ALT key.
如果您通过按住 ALT 键单击开始下载,则可以使用 Firefox 完全跳过提示。
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
...
profile.set_preference("browser.altClickSave", True)
...
element = driver.find_element_by_xpath("//a")
ActionChains(driver).key_down(Keys.ALT).click(element).key_up(Keys.ALT).perform()
This should download any file without needing to specify MIME type of anything.
这应该下载任何文件,而无需指定任何 MIME 类型。
回答by monolingual
The above solutions are great. But unfortunately my target file to download is a rare file type not in iana.org/assignments/media-types/media-types.xhtml. My solution is:
上面的解决方案很棒。但不幸的是,我要下载的目标文件是一种罕见的文件类型,不在 iana.org/assignments/media-types/media-types.xhtml 中。我的解决办法是:
Click on the download link in Firefox. Choose "Save File" and "Do this automatically for files like this from now on". This will create a a new "content type" in the Firefox profile when handling download.
Locate and copy your Firefox profile (https://support.mozilla.org/en-US/kb/back-and-restore-information-firefox-profiles). Named the profile folder as "prepared_firefox_profile".
- Read in the custom profile:
单击 Firefox 中的下载链接。选择“保存文件”和“从现在开始自动为此类文件执行此操作”。这将在处理下载时在 Firefox 配置文件中创建一个新的“内容类型”。
找到并复制您的 Firefox 配置文件 ( https://support.mozilla.org/en-US/kb/back-and-restore-information-firefox-profiles)。将配置文件文件夹命名为“prepared_firefox_profile”。
- 阅读自定义配置文件:
profile = FirefoxProfile("dir/to/your/firefox/profile/prepared_firefox_profile") profile.set_preference("browser.download.dir", "/your/desired/download/folder") driver = selenium.webdriver.Firefox(firefox_profile=profile)
profile = FirefoxProfile("dir/to/your/firefox/profile/prepared_firefox_profile") profile.set_preference("browser.download.dir", "/your/desired/download/folder") driver = selenium.webdriver.Firefox(firefox_profile=profile)