设置selenium以使用自定义配置文件,但它会默认打开

时间:2013-04-11 17:02:18

标签: python macos firefox selenium-webdriver

我正在尝试使用python和selenium来自动执行firefox中的一些任务。当我下载文件时,弹出窗口询问您是否要打开或保存,以及每次使用此类文件执行此操作的复选框。我发现除非您在网页修复程序上安装添加,否则复选框不起作用。我有正常安装,但当我使用python + selenium时,它使用没有添加的配置文件。

互联网指示我通过关闭Firefox,打开/ Applications / Utilities,然后输入命令来创建另一个配置文件:

/Applications/Firefox.app/Contents/MacOS/firefox-bin -p

然后我创建了一个我将用于selenium的新配置文件。我设置名称并更改文件夹名称。配置文件名称为“PTI_Auto_Profile”。文件夹路径显示如下:

/users/User/Library/Application Support/Firefox/Profiles/Selenium/

当我完成。我点击“启动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

我尝试过以下方法但没有成功。

profile = webdriver.FirefoxProfile(os.path.expanduser("~/Library/Application Support/Firefox/Profiles/Selenium/"))
driver = webdriver.Firefox(firefox_profile=profile) 

没有错误,默认用户。

profile = webdriver.FirefoxProfile(os.path.expanduser("~/Library/Application Support/Firefox/Profiles/Selenium/"))
driver = webdriver.Firefox(profile) 

没有错误,默认用户。

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)

错误:     fp.set_preference( “browser.download.dir”,GETCWD())     NameError:名称'getcwd'未定义

关于我做错的任何想法?谢谢!

P.S。我使用的是mac os x 10.8.2,python 2.7,firefox 20

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)

3 个答案:

答案 0 :(得分:25)

  

错误:fp.set_preference(“browser.download.dir”,getcwd())NameError:   名称'getcwd'未定义

getcwd()未定义。所以我假设您需要getcwd模块中的os

添加:import os,然后使用os.getcwd()调用。

或者您可以添加此功能的导入: from os import getcwd

包含适当导入的示例:

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)

答案 1 :(得分:15)

我做了以下事情:

Open Profile Directory

或者:

Linux:ls -d /home/$USER/.mozilla/firefox/*.default/查看用户个人资料目录

Mac:ls -d ~/Library/Application\ Support/Firefox/Profiles/*

输出:

/home/jmunsch/.mozilla/firefox/xfoyzfsb.default/
/home/jmunsch/.mozilla/firefox/yxjwk1py.default/

要加载自定义用户配置文件,我在firefox中创建了一个配置文件,然后使用python selenium webdriver代码执行以下操作:

def setUp(self):
    self.profile = webdriver.FirefoxProfile('/home/jmunsch/.mozilla/firefox/yxjwk1py.default')
    self.driver = webdriver.Firefox(self.profile)

系统信息:

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

还要注意

@Corey手动设置个人资料的答案

所有配置都可以在about:config下找到:

profile.set_preference('browser.download.folderList', 2)

答案 2 :(得分:5)

你应该添加:

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")

确实有效!