将 selenium 设置为使用自定义配置文件,但它保持默认打开状态 [英] Setting selenium to use custom profile, but it keeps opening with default

查看:18
本文介绍了将 selenium 设置为使用自定义配置文件,但它保持默认打开状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 python 和 selenium 在 Firefox 中自动执行一些任务.当我下载文件时,会弹出询问您是否要打开或保存,以及每次使用此类文件时都执行此操作的复选框.我发现该复选框不起作用,除非您在 Web Page Fixer 上安装该插件.我已经正常安装了,但是当我使用 python + selenium 时,它使用没有附加组件的配置文件.

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.

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

The internet has instructed me to create another profile by closing Firefox, opening /Applications/Utilities, then typing the command:

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

然后我创建一个新的配置文件,我将与 selenium 一起使用.我设置了名称并更改了文件夹名称.配置文件名称是PTI_Auto_Profile".文件夹路径显示如下:

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:

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

当我完成时.我单击启动 Firefox",终端屏幕上出现以下错误.

When I am done. I click 'Start Firefox', and the following error appears on my terminal screen.

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) 

没有错误,默认用户.

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: name 'getcwd' 未定义

Error: fp.set_preference("browser.download.dir",getcwd()) NameError: name 'getcwd' is not defined

对我做错了什么有任何想法吗?谢谢!

Any ideas on what I am doing wrong? Thank you!

附言我使用的是 mac os x 10.8.2、python 2.7、firefox 20

p.s. I am using mac os x 10.8.2, python 2.7, firefox 20

Corey Goldberg 提供的解决方案.这应该适用于所有 excel 版本.

SOLUTION PROVIDED BY Corey Goldberg. This should work for all excel versions.

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_preference("browser.download.dir",getcwd()) NameError:名称 'getcwd' 未定义

Error: fp.set_preference("browser.download.dir",getcwd()) NameError: name 'getcwd' is not defined

getcwd() 未定义.所以我假设你想要 os 模块中的 getcwd :

getcwd() is not defined. So I assume you want the getcwd from the os module:

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

或者你可以为这个函数添加导入:from os import getcwd

or you could just add the import for this function: 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)

这篇关于将 selenium 设置为使用自定义配置文件,但它保持默认打开状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆