身份验证Cookie不可与Selenium chromedriver重用 [英] Authentication cookies not reusable with selenium chromedriver
问题描述
我正在将硒3.141.0与chromedriver 86.0.4240.22一起使用,以登录网站并存储cookie.在下一个会话期间,我希望使用现有的cookie来防止再次登录.但是,它不起作用.一切似乎都运行良好,但是站点 https://primexbt.com 不会确认现有的cookie.我该怎么办?
I'm using selenium 3.141.0 with chromedriver 86.0.4240.22 to log into a website, store the cookies. During the next session I wish to use the existing cookies to prevent doing the log in again. However, it does not work. Everything seems to run fine, but the site https://primexbt.com does not ack the existing cookies. What can I do?
options = Options()
options.add_argument('--dns-prefetch-disable')
options.add_argument('--no-sandbox')
options.add_argument('--lang=en-US')
options.add_argument('--disable-setuid-sandbox')
chrome_prefs = {
'intl.accept_languages': 'en-US',
}
options.add_experimental_option('prefs', chrome_prefs)
self.driver = webdriver.Chrome(options=options)
# load cookies
cookies = get_cookies()
if cookies:
self.driver.get(self.URL_PRIMEXBT)
for cookie in cookies:
self.driver.add_cookie(cookie)
推荐答案
默认情况下-一个新的ChromeDriver会话将创建一个新的配置文件数据目录-这样您就不会再有来自前一个会话的cookie.我建议:
By default - a new ChromeDriver session creates a new profile data dir - so you don't have cookies from your previous session. I would suggest to either:
- 使用包含您的授权cookie的配置文件数据启动WebDriver
options.add_argument('user-data-dir')
- 通过直接http调用/API调用进行授权,绕过WebDriver(以加快测试速度)-并将响应cookie传递给WebDriver
- 或存储授权cookie并加载它们-例如使用此解决方案:如何使用Python + Selenium WebDriver保存和加载Cookie
这篇关于身份验证Cookie不可与Selenium chromedriver重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!