Python 将 selenium 设置为使用自定义配置文件,但它保持默认打开状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15954682/
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
Setting selenium to use custom profile, but it keeps opening with default
提问by ExperimentsWithCode
I am trying to use python and selenium to automate some tasks in firefox. When I download a file, that pop up comes up asking if you want to open or save, and a check box for do this every time with this kind of file. I have found that check box does not work unless you install the add on Web Page Fixer. I have that installed normally, but when I use python + selenium it uses a profile with no add ons.
我正在尝试使用 python 和 selenium 在 Firefox 中自动执行一些任务。当我下载文件时,会弹出询问您是否要打开或保存,以及每次使用此类文件时都执行此操作的复选框。我发现该复选框不起作用,除非您在 Web Page Fixer 上安装该插件。我已经正常安装了,但是当我使用 python + selenium 时,它使用没有附加组件的配置文件。
The internet has instructed me to create another profile by closing Firefox, opening /Applications/Utilities, then typing the command:
互联网指示我通过关闭 Firefox,打开 /Applications/Utilities,然后输入命令来创建另一个配置文件:
/Applications/Firefox.app/Contents/MacOS/firefox-bin -p
I then create a new profile that I will use with selenium. I set the name and change the folder name. The profile name is "PTI_Auto_Profile". The folder path displays as follows:
然后我创建一个新的配置文件,我将与 selenium 一起使用。我设置了名称并更改了文件夹名称。配置文件名称是“PTI_Auto_Profile”。文件夹路径显示如下:
/users/User/Library/Application Support/Firefox/Profiles/Selenium/
When I am done. I click 'Start Firefox', and the following error appears on my terminal screen.
当我完成。我单击“启动 Firefox”,终端屏幕上出现以下错误。
2013-04-11 11:57:30.422 firefox-bin[2248:707] invalid drawable
conf-room:~ User$ 2013-04-11 11:58:00.350 firefox-bin[2251:303] invalid drawable
I've tried the following to no success.
我已经尝试了以下没有成功。
profile = webdriver.FirefoxProfile(os.path.expanduser("~/Library/Application Support/Firefox/Profiles/Selenium/"))
driver = webdriver.Firefox(firefox_profile=profile)
No error, default user.
没有错误,默认用户。
profile = webdriver.FirefoxProfile(os.path.expanduser("~/Library/Application Support/Firefox/Profiles/Selenium/"))
driver = webdriver.Firefox(profile)
No error, default user.
没有错误,默认用户。
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir",getcwd())
fp.set_preference("browser.helperApps.neverAsk.saveToDisk","text/csv/xls")
driver = webdriver.Firefox(firefox_profile=fp)
Error: fp.set_preference("browser.download.dir",getcwd()) NameError: name 'getcwd' is not defined
错误:fp.set_preference("browser.download.dir",getcwd()) NameError: name 'getcwd' 未定义
Any ideas on what I am doing wrong? Thank you!
关于我做错了什么的任何想法?谢谢!
p.s. I am using mac os x 10.8.2, python 2.7, firefox 20
ps 我使用的是 mac os x 10.8.2、python 2.7、firefox 20
SOLUTION PROVIDED BY Corey Goldberg.This should work for all excel versions.
由 Corey Goldberg 提供的解决方案。这应该适用于所有 excel 版本。
import os
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', os.getcwd())
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', ('application/vnd.ms-excel'))
driver = webdriver.Firefox(profile)
采纳答案by Corey Goldberg
Error: fp.set_preference("browser.download.dir",getcwd()) NameError: name 'getcwd' is not defined
错误:fp.set_preference("browser.download.dir",getcwd()) NameError: name 'getcwd' 未定义
getcwd()is not defined. So I assume you want the getcwdfrom the osmodule:
getcwd()没有定义。所以我假设你想要getcwd来自os模块的:
add: import os, and then invoke with os.getcwd().
add: import os,然后使用os.getcwd().
or you could just add the import for this function:
from os import getcwd
或者你可以只为这个函数添加导入:
from os import getcwd
your example with the proper imports included:
您具有正确导入的示例包括:
import os
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', os.getcwd())
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/csv/xls')
driver = webdriver.Firefox(profile)
回答by jmunsch
I did the following:
我做了以下事情:


Or:
或者:
Linux: ls -d /home/$USER/.mozilla/firefox/*.default/to see user profile directories
Linux:ls -d /home/$USER/.mozilla/firefox/*.default/查看用户配置文件目录
Mac: ls -d ~/Library/Application\ Support/Firefox/Profiles/*
苹果电脑: ls -d ~/Library/Application\ Support/Firefox/Profiles/*
Output:
输出:
/home/jmunsch/.mozilla/firefox/xfoyzfsb.default/
/home/jmunsch/.mozilla/firefox/yxjwk1py.default/
To load a custom user profile I ran through creating a profile in firefox and then did the following with the python selenium webdriver code:
为了加载自定义用户配置文件,我在 firefox 中创建了一个配置文件,然后使用 python selenium webdriver 代码执行以下操作:
def setUp(self):
self.profile = webdriver.FirefoxProfile('/home/jmunsch/.mozilla/firefox/yxjwk1py.default')
self.driver = webdriver.Firefox(self.profile)
System Info:
系统信息:
Python 2.7.3 (default, Sep 26 2013, 20:08:41)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pkg_resources;pkg_resources.get_distribution("selenium").version
jmunsch@NE-522:~/Desktop/work$ firefox --version
Mozilla Firefox 26.0
also note
还要注意
@Corey's answer to manually set a profile
@Corey 对手动设置配置文件的回答
All of the configurables can be found under about:config:
所有可配置项都可以在以下位置找到about:config:
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.folderList', 2)
回答by Gili Hary
You should add this:
你应该添加这个:
profile.set_preference("browser.helperApps.neverAsk.openFile",
"text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml")
It does work!
它确实有效!

