我们如何使用本地Chrome的Cookie来使用Selenium登录? [英] How can we use local Chrome's cookies to login using Selenium?

查看:601
本文介绍了我们如何使用本地Chrome的Cookie来使用Selenium登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个测试套件,该套件可以使用Selenium登录到Google的Gmail网站。
问题是Selenium会打开一个新的Chrome窗口(类似于隐身模式),其中没有cookie。
我的代码:

I'm trying to create a test suite that does a login to Google's Gmail website with Selenium. The problem is that Selenium opens a new Chrome window (Like incognito mode) with no cookies. My code:

driver = webdriver.Chrome(executable_path=r'chromedriver.exe')
driver.get("https://hangouts.google.com/")

elem = driver.find_element_by_id("gb_70")
elem.send_keys(Keys.RETURN)

elem2 = driver.find_element_by_id("identifierId")
elem2.send_keys("mygmail@gmail.com")
elem2.send_keys(Keys.RETURN)
time.sleep(2)

elem3 = driver.find_element_by_xpath("//*[@class='whsOnd zHQkBf']")
elem3.send_keys("myPass")
elem3.send_keys(Keys.RETURN)

根据我在互联网上阅读的内容,可以创建2个会话,一个用于存储cookie,第二个用于使用这些cookie。
但这不是我想要的东西,我需要它来使用Chrome存储的cookie。

From what I read on the internet it is possible to create 2 sessions, one for storing the cookies and the second for using those cookies. But this is not something that I'm looking for, I need it to use the cookies that Chrome store.

我尝试手动读取Chrome的cookie (来自APPDATA),但似乎是由浏览器加密的。

I tried to read the Chrome's cookies manually (from APPDATA) but it seems that it is encrypted by the browser.

推荐答案

当您在会话之间需要cookie时,另一种方法,使用Chrome选项user-data-dir以便将文件夹用作配置文件,我运行:

When you need cookies from session to session there is another way to do it, use the Chrome options user-data-dir in order to use folders as profiles, I run:

chrome_options = Options()
chrome_options.add_argument("user-data-dir=selenium") 
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("www.google.com")

您可以在此处执行检查人机交互的登录信息,然后执行此操作我每次使用该文件夹启动Webdriver时都需要使用cookie。您还可以手动安装扩展,并在每个会话中使用它们。
我在运行Secon的时候,所有的cookie都在那里:

You can do here the logins that check for human interaction, I do this and then the cookies I need now every-time I start the Webdriver with that folder everything is in there. You can also manually install the Extensions and have them in every session. Secon time I run, all the cookies are there:

chrome_options = Options()
chrome_options.add_argument("user-data-dir=selenium") 
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("www.google.com") #Now you can see  the cookies, the settings, Extensions and the logins done in the previous session are present here

上一个会话中完成的设置,扩展名和登录名都在这里具有不同设置和Cookie的多个文件夹,无需加载,卸载Cookie,安装和卸载扩展,更改设置,通过代码更改登录名的扩展程序,因此无法中断程序逻辑,等等,这比必须通过代码来完成。

The advantage is you can use multiple folders with different settings and cookies, Extensions without the need to load, unload cookies, install and uninstall Extensions, change settings, change logins via code, and thus no way to have the logic of the program break, etc Also this is faster than having to do it all by code.

这篇关于我们如何使用本地Chrome的Cookie来使用Selenium登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆