保存/加载登录Cookie Selenium [英] Saving / Loading login cookies Selenium
问题描述
我想第一次保存登录Cookie,然后下次加载 这样我就不必每次都执行登录.
I want to save a login cookie in first time , then load it next times so the i won't have to perform a login each time.
有什么想法吗?
public void mControlClick()
{
var options = new ChromeOptions();
var chromeDriver = new ChromeDriver(options);
chromeDriver.Navigate().GoToUrl("http://website.com")
}
推荐答案
我遇到了类似的问题.看着开发人员窗口,我可以看到登录后发送了Cookie,但是通过javascript或其他内容进行的页面正在重定向,然后将控制权还给了程序.因此,我无法获取该cookie并将其保存.
I had a similar issue. Watching with the developer window, I could see that after login a cookie was being sent but then the page via javascript or something else was redirecting before returning control to the program. So, I was unable to get that cookie and save it off.
经过更多研究,我意识到该程序每次都从一个干净的会话开始(此答案很有帮助)因此持久性cookie根本不是持久性的.经过了进一步的研究,但是(通过碎片)为硒提供了解决方案,从而解决了我的问题.我实际上在使用python,但是我确定C#的选项仍然遵循
After more research I realised that the program was starting with a clean session each time (this answer helped a lot) so the persistent cookies weren't persistent at all. It took further research, but giving selenium (via splinter) a profile to work with resolved my issue. I am actually using python, but I'm sure the options still follow for C#
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("user-data-dir=" + tdir + "/chrome-session")
chrome_options.add_argument("--profile-directory=Default")
with Browser('chrome', headless=True, options=chrome_options) as browser:
这篇关于保存/加载登录Cookie Selenium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!