How to save and load cookies using Python + Selenium WebDriver

后端 未结 6 625
孤街浪徒
孤街浪徒 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: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)
    

提交回复
热议问题