【问题标题】:Selenium: FirefoxProfile fails with not found exceptionSelenium:FirefoxProfile 失败,未找到异常
【发布时间】:2011-10-02 22:16:25
【问题描述】:

我有以下代码,虽然我设置了profile_directory Firefox webdriver 仍然尝试将设置存储在/tmp 文件夹中

profile = FirefoxProfile(profile_directory = '/home/sultan/profiles')
profile.set_preference('network.proxy.http', scheme);
profile.set_preference('network.proxy.http_port', self.proxy.get('port'));

异常代码:

File "/home/sultan/Repository/Django/monitor/app/utils.py", line 79, in start
    request.perform(scan = scan, schedule = schedule)
File "/home/sultan/Repository/Django/monitor/app/request.py", line 230, in perform
    profile1 = FirefoxProfile(profile_directory = '/home/sultan/profiles')
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_profile.py", line 97, in __init__
    self._read_existing_userjs()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_profile.py", line 178, in _read_existing_userjs
    f = open(os.path.join(self.profile_dir, 'user.js'), "r")
IOError: [Errno 2] No such file or directory: '/tmp/webdriver-py-profilecopy/user.js'

我做错了什么还是需要为 selenium 添加特定的配置设置?

【问题讨论】:

  • /home/sultan/profiles 是否包含多个配置文件?
  • 是的,它应该包含多个配置文件,但现在它仍然将配置文件保存在 /tmp 中,原因是我尝试使用线程测试网站
  • 您不应该指向一个配置文件文件夹而不是包含多个配置文件的文件夹吗?
  • 是的,这个文件夹包含多个配置文件

标签: python firefox selenium webdriver


【解决方案1】:

我也有同样的问题。由于 FF5 在配置文件中没有“user.js” -> 我们不必阅读它。

所以打开 selenium/webdriver/firefox/firefox_profile.py 并在 def _read_existing_userjs(self) 之后添加 try except,如下所示:

def _read_existing_userjs(self):
    try:
        f = open(os.path.join(self.profile_dir, 'user.js'), "r")
    except IOError, e:
        print "We didn't find user.js in your profile, but that is ok"
        return

    tmp_usr = f.readlines()
    f.close()
    for usr in tmp_usr:
        matches = re.search('user_pref\("(.*)",\s(.*)\)', usr)
        self.default_preferences[matches.group(1)] = matches.group(2)

【讨论】:

  • 虽然,我不得不将 try except 块添加到其他几个方法中才能使其正常工作,即使在 9 年多之后它仍然像魅力一样工作。
猜你喜欢
  • 2011-10-04
  • 2016-11-04
  • 2021-04-09
  • 2012-06-26
  • 1970-01-01
  • 1970-01-01
  • 2017-12-24
  • 1970-01-01
相关资源
最近更新 更多