Python Selenium 使用 Firefox 配置文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37247336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium use of Firefox profile
提问by svgrafov
I try to use Selenium Webdriver and Python on Windows 10 system to make some automation of browser actions. 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. So I assumed that Selenium not really uses the profile, but just a copy of it.
我尝试在 Windows 10 系统上使用 Selenium Webdriver 和 Python 来实现浏览器操作的一些自动化。但是我有这个问题:Selenium 启动的 Firefox 窗口没有“看到”我已经登录并且目标站点将我发送到登录页面。所以我认为 Selenium 并没有真正使用配置文件,而只是它的一个副本。
I would like to know:
我想知道:
- Is my conclusion about actual use of copy of profile true?
- If 1. is true, is there a way to use really everything from existing profile?
- 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.
- 我关于实际使用配置文件副本的结论是否正确?
- 如果 1. 是真的,有没有办法真正使用现有配置文件中的所有内容?
- 如果我的结论不正确,请证明它并指出我可以找出哪些信息可以用于会话的方向,为什么 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")
回答by sowa
Selenium indeed uses a copy of the profile, though that ought not to cause any problems. I think your issue has more to do with session cookies vs. persistent cookies.
Selenium 确实使用了配置文件的副本,尽管这不会造成任何问题。我认为您的问题与会话 cookie 与持久 cookie 的关系更大。
On support.mozilla.orgis a list indicating what information is actually stored in your profile. Note that cookies are among these, however session-cookiesare not stored in cookies.sqlite which is the reason Selenium cannot rebuild your session since it does not appear in the profile.
在support.mozilla.org是表示什么信息实际保存在您的配置文件清单。请注意,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. I used the following script to test this out with gmail,
然而,许多站点在其登录页面上提供了一个remember-me
或一个stay-logged-in
选项,如果使用该选项,将存储一个持久性 cookie,通过该 cookie 可以恢复会话。我使用以下脚本用 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. 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
启用该选项的情况下登录 gmail 后运行此脚本时,Selenium 就可以访问我的收件箱。如果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 使用时不会出现在配置文件中。