How can I save all cookies in Python's Selenium WebDriver to a txt-file, then load them later? The documentation doesn't say much of anything about the getCookies function.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Just a slight modification for the code written by @Roel Van de Paar, as all credit goes to him. I am using this in Windows and it is working perfectly, both for setting and adding cookies:
my os is Windows 10, and the chrome version is 75.0.3770.100. I have tried the 'user-data-dir' solution, didn't work. try the solution of @ Eric Klien fails too. finally, I make the chrome setting like the picture, it works!but it didn't work on windows server 2012.
setting
this is code I used in windows, It works.
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:
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:
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 havin to do it all by code.
You can save the current cookies as a python object using pickle. For example:
and later to add them back:
Based on answer by @Eduard Florinescu but with newer code and missing import added: