简体   繁体   English

Selenium 使用 Firefox 配置文件

[英]Selenium use of Firefox profile

I try to use Selenium Webdriver and Python on Windows 10 system to make some automation of browser actions.我尝试在 Windows 10 系统上使用 Selenium Webdriver 和 Python 来实现浏览器操作的一些自动化。 But I have this problem: Selenium-started Firefox window doesn't "see" that I am already logged in and target site sends me to login page.但是我有这个问题:Selenium 启动的 Firefox 窗口没有“看到”我已经登录并且目标站点将我发送到登录页面。 So I assumed that Selenium not really uses the profile, but just a copy of it.所以我认为 Selenium 并没有真正使用配置文件,而只是它的一个副本。

I would like to know:我想知道:

  1. Is my conclusion about actual use of copy of profile true?我关于实际使用配置文件副本的结论是否正确?
  2. If 1. is true, is there a way to use really everything from existing profile?如果 1. 是真的,有没有办法真正使用现有配置文件中的所有内容?
  3. If my conclusion is not true, please prove it and point me to the direction where I can find out what information can be used for session, why Selenium could fail to send it and how to force it to do so actually.如果我的结论不正确,请证明它并指出我可以找出哪些信息可以用于会话的方向,为什么 Selenium 无法发送它以及如何实际强制它这样做。

Edit:编辑:

from selenium import webdriver
fp = webdriver.FirefoxProfile('C:/Users/<user name>/AppData/Roaming/Mozilla/Firefox/Profiles/abc3defghij2.ProfileName')
driver = webdriver.Firefox(fp)
driver.get("https://www.example.com/membersarea")

Selenium indeed uses a copy of the profile, though that ought not to cause any problems. Selenium 确实使用了配置文件的副本,尽管这不会导致任何问题。 I think your issue has more to do with session cookies vs. persistent cookies.我认为您的问题与会话 cookie 与持久 cookie 的关系更大。

On support.mozilla.org is a list indicating what information is actually stored in your profile. support.mozilla.org 上有一个列表,表明您的个人资料中实际存储了哪些信息。 Note that cookies are among these, however session-cookies are not stored in cookies.sqlite which is the reason Selenium cannot rebuild your session since it does not appear in the profile.请注意,cookies 也在其中,但是session-cookies没有存储在 cookies.sqlite 中,这就是 Selenium 无法重建会话的原因,因为它没有出现在配置文件中。

Many sites, however, offer a remember-me or a stay-logged-in option on their login page which, if used, will store a persistent cookie by which the session can be restored.但是,许多站点在其登录页面上提供了“ remember-me或“ stay-logged-in选项,如果使用该选项,将存储可以恢复会话的持久性 cookie。 I used the following script to test this out with gmail,我使用以下脚本用 gmail 测试了这一点,

from selenium import webdriver

url = "https://mail.google.com"
fp = webdriver.FirefoxProfile('/Users/<username>/Library/Application Support/Firefox/Profiles/71v1uczn.default')

driver = webdriver.Firefox(fp)
driver.get(url)

When I run this script after having logged into gmail with the stay-logged-in option enabled, then Selenium is able to access my inbox.当我stay-logged-in启用stay-logged-in选项stay-logged-in gmail 后运行此脚本时,Selenium 就可以访问我的收件箱。 If the stay-logged-in option is not enabled the session is destroyed upon closing my browser and thus Selenium cannot restore it either.如果未启用stay-logged-in选项,则关闭浏览器时会话将被销毁,因此 Selenium 也无法恢复它。

The screenshot below shows that session cookies are indeed not stored in cookies.sqlite and thus do not appear in the profile when used by Selenium.下面的屏幕截图显示会话 cookie 确实没有存储在 cookies.sqlite 中,因此当被 Selenium 使用时不会出现在配置文件中。

cookies.sqlite 和 firebug 中的 Firefox cookie

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

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