使用Firefox和Selenium webdriver不会记录历史记录 [英] Using Firefox with Selenium webdriver does not record history

查看:333
本文介绍了使用Firefox和Selenium webdriver不会记录历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 来自selenium import webdriver 
profile = webdriver。 FirefoxProfile('C:\\ Users \\\\\\\\\\\\\\\\\\\\\\\\\\\'') b $ bb = webdriver.Firefox(profile)



C:\用户\Mike\AppData\Roaming\Mozilla\Firefox\Profiles\kgbrptal.default\places.sqlite 我应该看到历史更新。但没有得到更新?



我期待所有行动被记录在配置文件数据库中。 问题

您正面临的问题是,Selenium会从您指定要使用的配置文件中创建一个新的临时配置文件。它这样做是因为它必须添加一个附件,它用来在浏览器和脚本之间建立通信。如果它没有将您的配置文件复制到新的位置,那么该附加配置将被添加到您提供给Selenium的配置文件中。许多用户会觉得这是不可接受的。因此,它会从您指定的那个创建一个新的临时配置文件,并在完成后删除它。

A解决方案



没有标志可以阻止删除临时配置文件。但是,您可以在调用 .quit()方法之前保存它。删除临时配置文件的代码

  try:
shutil.rmtree(self.profile.path)
if self .profile.temp文件夹不是无:
shutil.rmtree(self.profile.tempfolder)
,例外情况如下:
print(str(e))
WebDriver
分配给 b


$ b> c $ c>,你可以这样做:
$ b $ pre $ $ $ $ c $ shutil.copytree(b.profile.pathwhere / you / want / to / save / it,
ignore = shutil.ignore_patterns(parent.lock,
lock,.parentlock))
b.quit()



ignore 是避免复制一些锁定文件导致副本失败。



这里一个完整的例子:

pre $ 从selenium导入shutil
导入webdriver

profile = webdriver。 FirefoxProfile(path_to_your_profile)
driver = webdriver.Firefox(profile)
driver.get(http://google.com)
shutil.copytree(driver.profile.path,。 / here,
ignore = shutil.ignore_patterns(parent.lock,
lock,.parentlock))
driver.quit()

您需要将 path_to_your_profile 设置为实际路径。它会将临时配置文件复制到脚本的当前工作目录中的一个名为这里的子目录中。


I am running FF 39 with python Selenium webdriver 2.47.

from selenium import webdriver
profile = webdriver.FirefoxProfile('C:\\Users\\Mike\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\kgbrptal.default')
b = webdriver.Firefox(profile)

In C:\Users\Mike\AppData\Roaming\Mozilla\Firefox\Profiles\kgbrptal.default\places.sqlite I should see history updated. But nothing get updated?

I am expecting all actions been recorded in the profile database.

解决方案

The Problem

The problem you're facing is that Selenium creates a new and temporary profile from the profile you tell it to use. It does this because it has to add an add-on that it uses to establish communication between the browser and your script. If it did not copy your profile to a new location, the add-on would be added to the profile you gave to Selenium. Many users would find this unacceptable. So it creates a new temporary profile from the one you specify and deletes it after it is done.

A Solution

There is no flag you can give it to prevent the deletion of the temporary profile. However, you could save it before calling the .quit() method. The code that deletes the temporary profile goes:

        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))

If you assigned your WebDriver to b, you could do:

shutil.copytree(b.profile.path, "where/you/want/to/save/it", 
                ignore=shutil.ignore_patterns("parent.lock", 
                                              "lock", ".parentlock"))
b.quit()

The ignore is needed to avoid copying some lock files that would cause the copy to fail.

Here's a complete example:

import shutil
from selenium import webdriver

profile = webdriver.FirefoxProfile(path_to_your_profile)
driver = webdriver.Firefox(profile)
driver.get("http://google.com")
shutil.copytree(driver.profile.path, "./here",
                ignore=shutil.ignore_patterns("parent.lock", 
                                              "lock", ".parentlock"))
driver.quit()

You need to set path_to_your_profile to the actual path. It will copy the temporary profile to a subdirectory named here in the current working directory of the script.

这篇关于使用Firefox和Selenium webdriver不会记录历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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