3

So I am logging in to a site using splinter then I am grabbing the cookies after I am logged in and then saving it as a pickled object for later use.

def save_cookie(username, password):
    browser = Browser()
    browser.visit("https://somesite.com")

    browser.fill('username', username)
    browser.fill('password', password)

    login_button = browser.find_by_xpath('the_login_button_x_path')

    login_button.click()

    # make a file and save the pickled object.
    file_Name = "the_cookie"
    fileObject = open(file_Name,'wb') 
    pickle.dump(browser.cookies.all(verbose=True) ,fileObject) 
    fileObject.close()
    # so file is saved. 

    # I take screen shot so that I know that the user is logged in. 
    browser.driver.save_screenshot('screenshot.png')

So I want to be able to save that cookie data so that I can use it again later in another instance of splinter Browser() . this would allow me to login once instead of logging in every time I wanted to test a page that requires a logged in state.

def visit_site_as_logged_in_user():
    browser = Browser()

    #open the previously pickled cookies object and load it 
    the_previously_saved_cookies = pickle.load( open( "the_cookie", "rb"))
    #set the cookies on this new browser instance
    browser.cookies.add(the_previously_saved_cookies)

    #visit the site
    browser.visit("https://somesite.com")

    # I take screen shot so that I know that the user is logged in. 
    browser.driver.save_screenshot('screenshot.png')

So I expect for the user to visit the site and be in a logged in state since I am taking the exact same cookies from the first instance and applying it to a new instance. I am probably misunderstanding how splinter handles cookies though. The documentaion is not very descriptive . http://splinter.readthedocs.org/en/latest/cookies.html

When I look at the screen shot the user is just visiting the site and is not in a logged in state.

Spencer Cooley
  • 8,471
  • 16
  • 48
  • 63
  • This maybe similar to [this login cookie case](https://stackoverflow.com/a/54932483/1763157) – Bryan Mar 01 '19 at 03:43

1 Answers1

0

I had a similar situation, for me it was solved by visting an internal site after adding the cookies.

def visit_site_as_logged_in_user():
    browser = Browser()

    #open the previously pickled cookies object and load it 
    the_previously_saved_cookies = pickle.load( open( "the_cookie", "rb"))
    #set the cookies on this new browser instance
    browser.visit("https://somesite.com")  # see below
    browser.cookies.add(the_previously_saved_cookies)

    #visit the site
    browser.visit("https://somesite.com/internal")

    # I take screen shot so that I know that the user is logged in. 
    browser.driver.save_screenshot('screenshot.png')

this worked for me, probably depends on the site, but with this setup I did not had to fill in a login form again. Also I had to visit the base site befor setting the cookies, see Unable to add a cookie using Selenium & Splinter.

DiCaprio
  • 823
  • 1
  • 7
  • 24