当前位置: 首页 > 面试题库 >

Python / Selenium / Firefox:无法使用指定的配置文件路径启动Firefox

郭元明
2023-03-14
问题内容

我尝试使用指定的配置文件启动Firefox:

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 -这个目录是正确的Firefox配置文件目录,我用
firefox-bin --ProfileManager

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

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

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

为什么WebDriver忽略了Firefox的配置文件路径?使用铬不会有这样的问题。


问题答案:

我花了大约2个小时(是的,我太慢了)猜测为什么不起作用。我发现了为什么个人资料不保存回去。

当然,传递给FirefoxProfile("myprofile/full/path")它的配置文件是在运行时使用的,但是它并没有保存回去,因为(也许不是很明显)硒用于测试,并且测试应该在没有缓存,没有任何配置文件,尽可能干净的情况下运行。

配置文件功能(可能)是为了允许在运行测试之前安装某些扩展名和设置而创建的,而不是为了保存它们。

诀窍是打印出来print driver.firefox_profile.path。它与通常的确有所不同,其名称为 tmp / tmpOEs2RR /
webdriver-py-profilecopy,而
不仅仅是 tmp / tmpOEs2RR / ,因此请阅读您应该猜测的配置文件

现在,剩下的唯一事情就是找回它:)

运行此脚本,安装内容,编辑内容,然后再次运行;):

#!/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)

您可以遵循该架构,也可以根据需要简单地编辑FirefoxProfilea。



 类似资料:
  • 问题内容: 我正在尝试从Selenium 2升级到Selenium 3,但是旧的处理方式(既简单又快速)不再起作用了(而且似乎不存在该文档) 这是当前的程序,我要打开带有以下配置文件的Firefox驱动程序:SELENIUM 遗憾的是,它无法正常工作,并始终因错误而关闭: WebDriver.dll中发生类型为’System.InvalidOperationException’>的未处理异常 附加

  • 我已经使用python&Selenium在Ubuntu OS中设置了一个新创建的Firefox配置文件的路径。但是当我运行python脚本时,我会得到一个错误 我已将该文件的权限更改为755,但仍然出现错误,并尝试了sudo。 这是我的python脚本的开始:- 文件“default2.py”,第9行,在浏览器=webdriver.firefox(firefox_binary=binary)文件中

  • 问题内容: 我正在Eclipse中运行Selenium测试,但是无法加载自定义的Firefox配置文件。 大多数消息来源建议我需要像这样启动Selenium Server: 但是,当从Eclipse中启动我的测试时,它不会使用它-如果Selenium Server未运行,则测试将运行。 但是Selenium IDE(Firefox插件)为我生成的代码未使用该构造函数: 我应该在哪里设置Defaul

  • 我已经使用下面的命令启动了selenium服务器,我想使用定制的firefox配置文件。但是,它如何为我运行的每个测试用例在/tmp/目录中创建一个新的firefoxprofile呢。 /usr/bin/java-jar/usr/local/groundwork/selenium/java/selenium-server-standalone-2.33.0。jar-firefoxProfileTe

  • 问题内容: 在将selenium2.35与firefox的早期版本一起使用之前,我将其更新为firefox 26.0。我的测试以前运行良好,但现在最终出现以下错误。 我试图降级firefox的版本,但是无法从mozilla站点获得任何以前的版本。 我也尝试了所有selenium版本2.36、2.37、2.38、2.39,但它们似乎都与firefox 26.0不兼容。一旦我尝试将selenium2.

  • 问题内容: 我正在寻找Eclipse存储其启动配置的文件。我正在Ubuntu中进行一些Java开发。 我正在开发的一个可执行文件需要另一个可执行文件的输出作为其参数。可以这么说,“ B”的输出需要作为命令行参数传递给“ A”。在Eclipse中,我不想每次都手动更改“运行配置”。Eclipse将这些配置(尤其是参数)存储在哪里? 我在项目的extras文件夹中找到了“ eclipseArgumen