简体   繁体   English

如何强制Selenium与WebDriver一起使用永久Firefox配置文件?

[英]How to Force Selenium to use Permanent Firefox Profiles with WebDriver?

I'm using Selenium Web Driver API with Java. 我正在使用带有Java的Selenium Web Driver API。 Whenever I run Web Driver, it automatically creates a tmp Firefox Profile and executes all driver related code relative to the tmp profile. 每当我运行Web驱动程序时,它都会自动创建一个tmp Firefox配置文件,并执行所有与tmp配置文件相关的与驱动程序相关的代码。

My issue is not with the extra space this creates as asked in this question: How to stop Selenium from creating temporary Firefox Profiles using Web Driver? 我的问题不在于此问题所要求的额外空间: 如何阻止Selenium使用Web Driver创建临时Firefox配置文件? and I know I could call driver.quit to clear the resources used by the tmp profile. 而且我知道我可以调用driver.quit来清除tmp配置文件使用的资源。

Rather, my issue is I want to use the permanent Firefox Profile so that the next time I launch this FF profile I inherit all the cookies and cache of the previous profile. 相反,我的问题是我想使用永久性的Firefox配置文件,以便下次启动该FF配置文件时,我继承了前一个配置文件的所有cookie和缓存。 I know I can manually save and add cookies each time but this is tedious. 我知道我可以每次手动保存和添加Cookie,但这很繁琐。

So, is there any way to force Firefox not to create a tmp profile and launch Firefox as if I was browsing normally using the permanent profile? 那么,有什么方法可以迫使Firefox不要创建tmp配置文件并像我在使用永久配置文件正常浏览时一样启动Firefox? I just want it to launch as if I was browsing like a normal user. 我只是希望它像正常用户一样在浏览时启动。

Thanks 谢谢

Generally Selenium do not support cross-session cookies. 通常,Selenium不支持跨会话cookie。

Most easy way is to use Serialization. 最简单的方法是使用序列化。 You need to create wrapper class around selenium's cookie and make it serializable. 您需要围绕硒的cookie创建包装器类,并使其可序列化。 And create class CookiesManager where will be 2 methods: SaveSession() -- to save and RestoreSession() - to restore from serialized file. 并创建类CookiesManager,其中将包含2种方法:SaveSession()-保存和RestoreSession()-从序列化文件还原。

Another way is to save some cookies information into some temp cookies file. 另一种方法是将一些cookie信息保存到一些临时cookie文件中。 Like.... Csv or XML. 就像...。CSV或XML。 Sample of this way you can see here: Keep user logged in - save cookies using web driver but only for c#. 您可以在此处看到这种方式的示例: 保持用户登录状态-使用Web驱动程序保存cookie,但仅适用于c#。


Using specific profile: 使用特定的配置文件:

ProfilesIni profile = new ProfilesIni();

FirefoxProfile myprofile = profile.getProfile("profileToolsQA");

WebDriver driver = new FirefoxDriver(myprofile);

to create some additional profile for firefox you need to run firefox profile manager by the following way: firefox.exe -p 要为firefox创建一些其他配置文件,您需要通过以下方式运行firefox配置文件管理器: firefox.exe -p

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM