我使用python Selenium webdriver 2.47运行FF 39。
from selenium import webdriver
profile = webdriver.FirefoxProfile('C:\\Users\\Mike\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\kgbrptal.default')
b = webdriver.Firefox(profile)
在C:\Users\Mike\AppData\Roaming\Mozilla\Firefox\Profiles\kgbrptal.default\places.sqlite
我应该看到历史记录已更新。但什么都没有更新?
我希望所有操作都记录在配置文件数据库中。
答案 0 :(得分:1)
您遇到的问题是Selenium会从您告诉它使用的配置文件中创建一个新的临时配置文件。这样做是因为它必须添加一个用于在浏览器和脚本之间建立通信的附加组件。如果它没有将您的配置文件复制到新位置,则该加载项将添加到您为Selenium提供的配置文件中。许多用户会发现这是不可接受的。因此,它会从您指定的临时配置文件创建一个新的临时配置文件,并在完成后将其删除。
没有标记可以提供它以防止删除临时配置文件。但是,您可以在调用.quit()
方法之前保存它。删除临时配置文件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))
如果您已将WebDriver
分配给b
,则可以执行以下操作:
shutil.copytree(b.profile.path, "where/you/want/to/save/it",
ignore=shutil.ignore_patterns("parent.lock",
"lock", ".parentlock"))
b.quit()
需要ignore
以避免复制某些会导致副本失败的锁定文件。
这是一个完整的例子:
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()
您需要将path_to_your_profile
设置为实际路径。它会将临时配置文件复制到脚本当前工作目录中名为here
的子目录中。