有没有办法使用Selenium让OAuth2的cookies永久有效。

huangapple go评论151阅读模式
英文:

Is there a way to keep OAuth2 cookies valid indefinitely with selenium

问题

I'm using selenium to access a page that requires logging in. Currently I'm doing something like this:

import json
from selenium.webdriver import Chrome

cookies_path = 'tmp/cookies.json'
cookies = json.load(open(cookies_path))
driver = Chrome()
driver.get('https://www.example.com/')
for cookie in cookies:
    driver.add_cookie(cookie)
driver.get('https://www.example.com/user-data')
# do stuff
json.dump(driver.get_cookies(), open(cookies_path, 'w'))
driver.quit()

The script is scheduled to run every hour. However, it can only run successfully 23 times. At the 24th time, when driver.get('https://www.example.com/user-data') is executed, the driver will be redirected to the login page and my code breaks. Then I have to manually log in again, manually save the cookies, but the new cookies will still be invalid after 24 hours. But on my own laptop, I can stay logged in on this website indefinitely and have never been redirected to log in. I tried skipping expired ones when adding cookies to the driver, but it did not help.

FYI the website is using OAuth2, and I guess my cookies expire when the session token expires on the server. Is there a way to keep the cookies valid indefinitely with selenium like with my browser?

Edit:

Just saw this person had the same problem as mine, but no answer so far.

英文:

I'm using selenium to access a page that requires logging in. Currently I'm doing something like this:

import json
from selenium.webdriver import Chrome

cookies_path = 'tmp/cookies.json'
cookies = json.load(open(cookies_path))
driver = Chrome()
driver.get('https://www.example.com/')
for cookie in cookies:
    driver.add_cookie(cookie)
driver.get('https://www.example.com/user-data')
# do stuff
json.dump(driver.get_cookies(), open(cookies_path, 'w'))
driver.quit()

The script is scheduled to run every hour. However, it can only run successfully 23 times. At the 24th time, when driver.get('https://www.example.com/user-data') is executed, the driver will be redirected to the login page and my code breaks. Then I have to manually log in again, manually save the cookies, but the new cookies will still be invalid after 24 hours. But on my own laptop, I can stay logged in on this website indefinitely and have never been redirected to log in. I tried skipping expired ones when adding cookies to the driver, but it did not help.

FYI the website is using OAuth2 and I guess my cookies expire when the session token expires on the server. Is there a way to keep the cookies valid indefinitely with selenium like with my browser?

Edit:

Just saw this person had the same problem as mine, but no answer so far.

答案1

得分: 1

因为 Selenium 的情况下,cookie 在 24 小时后会过期,而在您的笔记本电脑上,浏览器可能会将会话令牌存储在安全会话存储中,与 cookie 分开,并且会持久保存。您可以通过在本地机器上将会话令牌存储在安全存储中(序列化到文件中),然后每次运行脚本时检索(反序列化)它来实现相同的效果。

英文:

Its because cookies are expiring after 24 hours in case of selenium whereas on your laptop the browser is likely storing sessions token in secure session storage which is separate from cookies and which persists. You can achieve the same effect with selenium by storing the session token in a secure storage on your local machine(serialise in a file) and then retrieving(deserialise) it each time you run the script.

答案2

得分: 0

感谢 @Saheed Hussain 的回答和这个答案,我通过以下方式解决了它:

from selenium.webdriver import Chrome, ChromeOptions

options = ChromeOptions()
options.add_argument('--user-data-dir=tmp/user_data')
options.add_argument('--profile-directory=Profile 1')
driver = Chrome(options=options)
英文:

Thanks to @Saheed Hussain's answer and this answer, I solved it by doing

from selenium.webdriver import Chrome, ChromeOptions

options = ChromeOptions()
options.add_argument('--user-data-dir=tmp/user_data')
options.add_argument('--profile-directory=Profile 1')
driver = Chrome(options=options)

huangapple
  • 本文由 发表于 2023年4月10日 22:34:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定