Warm tip: This article is reproduced from serverfault.com, please click

其他-如何在 python 中为 selenium webdriver 导入和导出 FireFox 配置文件

(其他 - How to import and export FireFox profile for selenium webdriver in python)

发布于 2013-04-03 12:47:30

如何导入和导出 webdriver FireFox 配置文件?

我想做的是:

from selenium import webdriver

#here I want to import the FF profile from a path

if profile:
  driver = webdriver.Firefox(profile)
else:
  #this is the way I get the WebDriver currently
  driver = webdriver.Firefox()

#doing stuff with driver

#Here I want to save the driver's profile 
#so I could import it the next time
Questioner
dnlcrl
Viewed
0
Silas Ray 2013-04-03 21:33:16

你必须决定存储缓存配置文件的位置,然后使用os中的函数检查该位置是否有文件,并加载它。要首先缓存配置文件,你应该能够从 获取配置文件的路径webdriver.firefox_profile.path,然后将内容复制到你的缓存位置。

综上所述,我真的建议不要这样做。通过缓存在测试运行时创建的配置文件,你可以根据以前的行为改变测试,这意味着它不再是孤立的并且可靠地可重复。我建议你与测试分开创建配置文件,然后始终将其用作基本配置文件。这使你的测试具有可预测的可重复性。Selenium 甚至设置为可以很好地与此模式配合使用,因为它实际上并不使用你提供的配置文件,而是复制它并使用副本来启动浏览器。