Reputation: 35873
I would like to check if my "Keep me signed in" checkbox working properly. So I create a new driver instance, open the login page etc...
However to check if "Keep me signed in" is working properly now I have to close the browser window, and reopen it. The trap is that this case it is opened from the "virgin" profile, so regardless what the previous instance did, the state will be the same stater state.
Question
Is there any way to close the browser window, and reopen it where in the previous session closed?
Upvotes: 1
Views: 964
Reputation: 3790
Using Python:
Lets use https://stackoverflow.com/
as an example. The scrip below will navigate to this website and give you time to login. Then it will close and open again and load the cookies from the pervious session so you are logged in. You may not need a whole minuet to login, just adjust that sleep to what you are looking for. The driver.close()
at the end is commented out so you can click around and make sure you are logged in as you expect.
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import pickle
import os
service = Service('C:\\Py\\geckodriver.exe')
driver = webdriver.Firefox(service=service)
driver.get('https://stackoverflow.com/')
time.sleep(60)
# #########################
#
# #LOGIN TO STACKOVERFLOW
#
# ########################
pickle.dump(driver.get_cookies(), open("C:/Py/cookie/cookies.pkl","wb"))
driver.close()
time.sleep(2)
driver = webdriver.Firefox(service=service)
driver.get("https://stackoverflow.com/")
cookies = pickle.load(open("C:/Py/cookie/cookies.pkl", "rb"))
for cookie in cookies:
driver.add_cookie(cookie)
driver.get("https://stackoverflow.com/")
time.sleep(2)
# driver.close()
Upvotes: 1