Python / Selenium / Firefox:无法使用指定的配置文件路径启动firefox [英] Python / Selenium / Firefox: Can't start firefox with specified profile path

查看:2113
本文介绍了Python / Selenium / Firefox:无法使用指定的配置文件路径启动firefox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

  firefox_profile = webdriver.FirefoxProfile('/ Users / p2mbot / projects / test / firefox_profile')
driver = webdriver.Firefox(firefox_profile = firefox_profile)

driver.get('http://google.com')
time.sleep(60 )
driver.quit()

/ Users / p2mbot /这个目录是正确的firefox profile dir,我用 firefox-bin --ProfileManager

创建了这个目录。 b
$ b

但是,当我通过selenium检查firefox中的缓存页面时,它有不同的缓存路径:

 存储磁盘位置:/ var / folders / jj / rdpd1ww53n95y5vx8w618k3h0000gq / T / tmpp2ahq70_ / webdriver-py-profilecopy / cache2 

如果通过firefox-bin --ProfileManager运行firefox并选择配置文件,它将显示在about:cache页面正确路径 / Users / p2mbot / projects / test / firefox_profile



为什么webdriver忽略firefox的配置文件路径?有了chrome,就没有这个问题了。

解决方案

我花了大概2个小时工作。当然,配置文件传递给 FirefoxProfile(myprofile / full / path) / code>在运行时使用,但是由于(可能不是很明显)selenium用于测试,所以测试应该在没有缓存的情况下运行,根本没有配置文件,尽可能的干净。



配置文件功能可能(也许)可以在运行测试之前安装某些扩展和设置,但不能保存。



诀窍是打印出 print driver.firefox_profile.path 。它与通常的名称不同,名称为 tmp / tmpOEs2RR / webdriver-py-profilecopy ,而不是 tmp / tmpOEs2RR / ,因此,您应该猜测一个配置文件正在使用。



现在,唯一剩下的就是把它拿回来:



运行这个脚本,安装一些东西,编辑一些东西然后再运行它):



$ p $ #!/ usr / bin / env python
#! - * - coding:utf-8 - * -

从selenium导入selenium
导入webdriver

导入os,sys,time

#1-设置配置文件
profile = os.path.dirname(sys.argv [0])+/ selenita
fp = webdriver.FirefoxProfile(配置文件)
驱动程序= webdriver。 Firefox(firefox_profile = fp)

#2 - 获取tmp文件位置
profiletmp = driver.firefox_profile.path

...但是...当前配置文件是一个复制原始配置文件:/
printrunning profile+ profiletmp

driver.get(http://httpbin.org)
time.sleep(2)
raw_input(完成后按一个键)#我已经安装了扩展名

#3-然后保存
print保存配置文件+ profiletmp +到+ profile
如果os.system(cp -R+ profiletmp +/ *+ profile):
print应该被复制:/


driver.quit()
sys.exit(0)

U can遵循该架构或简单地编辑FirefoxProfilea只要你的需要。


I try to start firefox with specified profile:

firefox_profile = webdriver.FirefoxProfile('/Users/p2mbot/projects/test/firefox_profile')
driver = webdriver.Firefox(firefox_profile=firefox_profile)

driver.get('http://google.com')
time.sleep(60)
driver.quit()

/Users/p2mbot/projects/test/firefox_profile -- this directory is correct firefox profile dir, I created it with firefox-bin --ProfileManager

But when I check about:cache page in firefox via selenium, it has different path for cache:

Storage disk location:  /var/folders/jj/rdpd1ww53n95y5vx8w618k3h0000gq/T/tmpp2ahq70_/webdriver-py-profilecopy/cache2

If run firefox via firefox-bin --ProfileManager and choose the profile, it will show at about:cache page correct path /Users/p2mbot/projects/test/firefox_profile

Why webdriver ignore profile path for firefox? With chrome there is not such problem.

解决方案

I have spent around 2 hours (yes im so slow) guessing why didn't work. I've found why profile doesn't save back.

Certainly, the profile passed to FirefoxProfile("myprofile/full/path") is used on the run, but, it's not saved back because (and maybe not obvious) selenium is for testing and testings should run with no cache, no profiles at all, clean as possible.

Profile capability was (maybe) build to allow install certain extensions and settings before run test, but not for save them.

The trick was to print out print driver.firefox_profile.path. It's does differ from usual ones, with name tmp/tmpOEs2RR/webdriver-py-profilecopy instead instead of just tmp/tmpOEs2RR/, so reading that you should guess that a profile is being used.

Now, the only remaining thing is to get it back :)

Run this script, install something, edit something then run it again ;) :

#!/usr/bin/env python
#! -*- coding: utf-8 -*-

import selenium
from selenium import webdriver

import os, sys, time

# 1- set profile
profile = os.path.dirname(sys.argv[0]) + "/selenita"
fp = webdriver.FirefoxProfile(profile)
driver = webdriver.Firefox(firefox_profile=fp)

# 2- get tmp file location
profiletmp = driver.firefox_profile.path

# but... the current profile is a copy of the original profile :/
print "running profile " + profiletmp

driver.get("http://httpbin.org")
time.sleep(2)
raw_input("Press a key when finish doing things") # I've installed an extension

# 3- then save back
print "saving profile " + profiletmp + " to " + profile
if os.system("cp -R " + profiletmp + "/* " + profile ):
    print "files should be copied :/"


driver.quit()
sys.exit(0)

U can follow that schema or simple edit the FirefoxProfilea accordingly to your needs.

这篇关于Python / Selenium / Firefox:无法使用指定的配置文件路径启动firefox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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