Python 将selenium设置为使用自定义配置文件,但它会保持默认打开状态
我正在尝试使用python和selenium自动化firefox中的一些任务。当我下载一个文件时,会弹出一个对话框,询问您是否要打开或保存,并且会出现一个复选框,用于每次使用此类文件时都执行此操作。我发现该复选框不起作用,除非您安装了附加网页修复程序。我已经正常安装了它,但是当我使用python+selenium时,它使用了一个没有附加组件的概要文件 互联网指示我关闭Firefox,打开/Applications/Utilities,然后键入以下命令,创建另一个配置文件:Python 将selenium设置为使用自定义配置文件,但它会保持默认打开状态,python,macos,firefox,selenium-webdriver,Python,Macos,Firefox,Selenium Webdriver,我正在尝试使用python和selenium自动化firefox中的一些任务。当我下载一个文件时,会弹出一个对话框,询问您是否要打开或保存,并且会出现一个复选框,用于每次使用此类文件时都执行此操作。我发现该复选框不起作用,除非您安装了附加网页修复程序。我已经正常安装了它,但是当我使用python+selenium时,它使用了一个没有附加组件的概要文件 互联网指示我关闭Firefox,打开/Applications/Utilities,然后键入以下命令,创建另一个配置文件: /Applicatio
/Applications/Firefox.app/Contents/MacOS/firefox-bin -p
然后我创建了一个新的配置文件,我将与selenium一起使用。我设置了名称并更改了文件夹名称。配置文件名为“PTI\U自动配置文件”。文件夹路径显示如下:
/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)
无错误,默认用户
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_首选项(“browser.download.dir”,getcwd())
NameError:未定义名称“getcwd”
你知道我做错了什么吗?谢谢大家!
p、 我使用的是MacOSX10.8.2,Python2.7,Firefox20
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)
错误:fp.set_首选项(“browser.download.dir”,getcwd())名称错误:
未定义名称“getcwd”
未定义getcwd()
。因此,我假设您希望从os
模块获得getcwd
:
importos
,然后使用os.getcwd()
调用
或者,您可以添加此函数的导入:
从操作系统导入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)
我做了以下工作:
或:
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手动设置配置文件的答案
所有可配置文件都可以在关于:config
下找到:
profile.set\u首选项('browser.download.folderList',2)
您应该添加以下内容:
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")
它确实有用 谢谢大家!!我发现最底层首选项的最后一部分是MIME类型。通过将“text/csv/xls”替换为“application/vnd.ms excel”,我成功地下载了excel文件,没有弹出窗口。它适用于所有excel文件版本。不幸的是,此代码对我的浏览器没有影响。我仍然看到那个弹出窗口。我在Win7-64位和python 2.7上工作;你能帮我一下吗?试试看:driver=webdriver.Firefox(Firefox_profile=profile)找到了一个链接如何为Firefox创建selenium配置文件:它在python3.5、Ubuntu、FF48上运行。在代码中手动设置配置文件无效。