0

I am trying to load a cookie saved with selenium WebDriver in order to automatically login into GitHub account. Any ideas what i am doing wrong? This is the script output: selenium.common.exceptions.UnableToSetCookieException: Message: unable to set cookie

chrome.find_element_by_xpath("//*[@id='login_field']").send_keys("*********")
chrome.find_element_by_xpath("//*[@id='password']").send_keys("***********")
chrome.find_element_by_xpath("//*[@id='login']/div[4]/form/div/input[12]").click()
time.sleep(3)
save_cookies(chrome,"saved_cookies.txt")
chrome.quit

time.sleep(2)
chrome1 = webdriver.Chrome()
load_cookies(chrome,"saved_cookies.txt")
chrome1.get("https://github.com")

These are the functions I use:

def save_cookies(driver, location):
    pickle.dump(driver.get_cookies(), open(location, "wb"))

def load_cookies(driver, location, url=None):
    cookies = pickle.load(open(location, "rb"))
    driver.delete_all_cookies()
    url = "https://github.com" if url is None else url
    driver.get(url)
    for cookie in cookies:
        driver.add_cookie(cookie)
jabbson
  • 4,390
  • 1
  • 13
  • 23

1 Answers1

1

Your code looks good to me. However one possible improvement would be while saving the cookies using pickle.dump() instead of a text file ideally you should be using .pkl file. As an example, saved_cookies.pkl.

Your effective line of code will be:

save_cookies(chrome,"saved_cookies.pkl")

and

load_cookies(chrome,"saved_cookies.pkl")

References

You can find a couple of relevant detailed discussion in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Unfortunately it did not solved the problem. I have some feeling that there are some incompatibilities between Python and Selenium versions. These are the versions I use: Name: selenium Version: 4.1.3 Python 3.10.4 – Catalin Diaconu Apr 30 '22 at 08:32