How to save and load cookies using Python + Selenium WebDriver

后端 未结 6 615
孤街浪徒
孤街浪徒 2020-11-22 07:52

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

相关标签:
6条回答
  • 2020-11-22 08:22

    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 need to: from selenium.webdriver.chrome.options import Options
    chrome_options = Options()
    chrome_options.add_argument("user-data-dir=selenium") 
    driver = webdriver.Chrome(chrome_options=chrome_options)
    driver.get("www.google.com")
    

    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:

    #you need to: from selenium.webdriver.chrome.options import Options    
    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, etc, and the logins done in the previous session are present here. 
    

    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.

    0 讨论(0)
  • 2020-11-22 08:23

    this is code I used in windows, It works.

     for item in COOKIES.split(';'):
                name,value = item.split('=',1)
                name=name.replace(' ','').replace('\r','').replace('\n','')
                value = value.replace(' ','').replace('\r','').replace('\n','')
                cookie_dict={  
                        'name':name,
                        'value':value,
                        "domain": "",  # google chrome
                        "expires": "",
                        'path': '/',
                        'httpOnly': False,
                        'HostOnly': False,
                        'Secure': False
                        }
                self.driver_.add_cookie(cookie_dict)
    
    0 讨论(0)
  • 2020-11-22 08:26

    You can save the current cookies as a python object using pickle. For example:

    import pickle
    import selenium.webdriver 
    
    driver = selenium.webdriver.Firefox()
    driver.get("http://www.google.com")
    pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))
    

    and later to add them back:

    import pickle
    import selenium.webdriver 
    
    driver = selenium.webdriver.Firefox()
    driver.get("http://www.google.com")
    cookies = pickle.load(open("cookies.pkl", "rb"))
    for cookie in cookies:
        driver.add_cookie(cookie)
    
    0 讨论(0)
  • 2020-11-22 08:28

    Based on answer by @Eduard Florinescu but with newer code and missing import added:

    $ cat work-auth.py 
    #!/usr/bin/python3
    
    # Setup:
    # sudo apt-get install chromium-chromedriver
    # sudo -H python3 -m pip install selenium
    
    import time
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    chrome_options = Options()
    chrome_options.add_argument("--user-data-dir=chrome-data")
    driver = webdriver.Chrome('/usr/bin/chromedriver',options=chrome_options)
    chrome_options.add_argument("user-data-dir=chrome-data") 
    driver.get('https://www.somedomainthatrequireslogin.com')
    time.sleep(30)  # Time to enter credentials
    driver.quit()
    
    $ cat work.py 
    #!/usr/bin/python3
    
    import time
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    chrome_options = Options()
    chrome_options.add_argument("--user-data-dir=chrome-data")
    driver = webdriver.Chrome('/usr/bin/chromedriver',options=chrome_options)
    driver.get('https://www.somedomainthatrequireslogin.com')  # Already authenticated
    time.sleep(10)
    driver.quit()
    
    0 讨论(0)
  • 2020-11-22 08:39

    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:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    chrome_options = Options()
    chrome_options.add_argument("--user-data-dir=chrome-data")
    driver = webdriver.Chrome('chromedriver.exe',options=chrome_options)
    driver.get('https://web.whatsapp.com')  # Already authenticated
    time.sleep(30)
    
    0 讨论(0)
  • 2020-11-22 08:42

    Remember, you can only a add cookie for the current domain. If you wanna add a cookie for your Google account, do

    browser.get('http://google.com')
    for cookie in cookies:
        browser.add_cookie(cookie)
    
    0 讨论(0)
提交回复
热议问题