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)

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.