【问题标题】:Selenium - Retaining firefox cache and history filesSelenium - 保留 Firefox 缓存和历史文件
【发布时间】:2016-08-10 16:52:55
【问题描述】:

有没有办法禁用 Selenium 在启动 Firefox 时创建临时目录和配置文件?

我完全理解为什么 Selenium 会这样做。我只是在尝试使用它创建 Firefox 缓存和历史记录以用于计算机取证培训目的。为此,我使用原始用户帐户设置了一个干净的虚拟机。我现在可以使用 selenium API 运行 Python 脚本来启动 Firefox,访问几个网页并关闭。

问题是,它什么也没留下。如果您在最初的目的中使用 selenium,这当然非常好,但它会通过删除所有内容来阻碍我的工作。

那么有没有办法禁用临时配置文件创建并启动 Firefox,因为如果用户在没有 Selenium 的情况下运行它会启动。

下午 5:34 补充: Java API 文档提到了一个系统属性 webdriver.reap_profile 可以防止删除临时文件。我去了问题的根源,它似乎没有出现在 Python WebDriver 类中:

def quit(self):
        """Quits the driver and close every associated window."""
        try:
            RemoteWebDriver.quit(self)
        except (http_client.BadStatusLine, socket.error):
            # Happens if Firefox shutsdown before we've read the response from
            # the socket.
            pass
        self.binary.kill()
        try:
            shutil.rmtree(self.profile.path)
            if self.profile.tempfolder is not None:
                shutil.rmtree(self.profile.tempfolder)
        except Exception as e:
            print(str(e))

退出时删除文件似乎是无条件的。我将通过注入来解决这个问题

return self.profile.path

就在 self.binary.kill() 之后。这可能会破坏各种事情并且是一件可怕的事情,但它似乎完全符合我的要求。返回值告诉调用函数/tmp 下临时目录的随机名称。不优雅,但似乎工作。

【问题讨论】:

  • 即使 firefox 正常启动,如果没有 selenium,它也会使用现有的配置文件目录,或者会创建一个 (reference)。
  • 但是当我正常启动它时,它会创建~/.cache/mozilla/firefox/xxxxx目录,xxxxx在.mozilla/firefox中是一样的。通过 Selenium 启动时,不会出现此目录。这是我的问题。缓存似乎被禁用或出现在临时位置并立即被删除。
  • 谢谢,现在更清楚了 - 您在谈论缓存,而不是配置文件。明白了。
  • 是的,很抱歉造成混乱。它似乎创建了包含配置文件和缓存信息的/tmp/..hexid.../ dir,但是一旦我调用quit(),它就会被删除。如果我不退出 firefox,缓存状态是不干净的,并且很少存储在那里。这实际上是我试图用 selenium 解决的问题:在 firefox 操作后彻底关闭。只要我能防止它被删除,我就可以使用 /tmp 目录。
  • 并非如此。请参阅我稍后的帖子。

标签: firefox selenium


【解决方案1】:

下午 5:34 补充:Java API 文档提到了一个系统属性 webdriver.reap_profile,它可以防止删除临时文件。我去了问题的根源,它似乎没有出现在 Python WebDriver 类中:

def quit(self):
        """Quits the driver and close every associated window."""
        try:
            RemoteWebDriver.quit(self)
        except (http_client.BadStatusLine, socket.error):
            # Happens if Firefox shutsdown before we've read the response from
            # the socket.
            pass
        self.binary.kill()
        try:
            shutil.rmtree(self.profile.path)
            if self.profile.tempfolder is not None:
                shutil.rmtree(self.profile.tempfolder)
        except Exception as e:
            print(str(e))

退出时删除文件似乎是无条件的。我将通过注入解决这个问题

return self.profile.path

在 self.binary.kill() 之后的 /usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py 中。这可能会破坏各种事情并且是一件可怕的事情,但它似乎完全符合我的要求。返回值告诉调用函数/tmp 下临时目录的随机名称。不优雅,但在重新编译后似乎可以使用。

如果存在更优雅的解决方案,我很乐意将其标记为正确的解决方案。

【讨论】:

    猜你喜欢
    • 2021-11-28
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    • 2014-07-06
    • 2018-10-23
    • 1970-01-01
    相关资源
    最近更新 更多