user15682361
user15682361

Reputation:

Python use same browser session and use cookies

I'm running a simple scrape code to scrape a few lines from a website. The problem is that Python always opens a new Chrome window and logs in again every time I run the bot.

So, I read online and created a Chrome profile. Now it opens the Chrome profile, but it still asks me to log in to the website. I guess I need to save cookies. I tried some YouTube tutorials, but I couldn't figure out how to save the cookies. I'm a complete noob, so can anyone explain to me how to do so?

This is my code:

    ##First login and cookie save

         options = Options() 
service = Service("C:\\Program Files (x86)\\chromedriver.exe")
options.add_argument("user-data-dir=C:\\Users\\parko\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 2")
driver = webdriver.Chrome(service=service, options=options)
options.add_argument("user-data-dir=C:\\Users\\parko\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 2")
driver.get("https://exmaplewebsitelogin.com")
    pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))

search = driver.find_element(By.NAME, 'fm-login-id')
search.send_keys("[email protected]")
search.send_keys(Keys.RETURN)

time.sleep(3)
search = driver.find_element(By.NAME, 'fm-login-password')
search.send_keys("exmaplepassword!")
search.send_keys(Keys.RETURN)

time.sleep(3)

search = driver.find_element(By.CLASS_NAME, 'fm-button')
search.send_keys(Keys.RETURN)
time.sleep(15)

time.sleep(3)
driver.quit()

## visiting the website after cookie saved
options = Options() 
service = Service("C:\\Program Files (x86)\\chromedriver.exe")
options.add_argument("user-data-dir=C:\\Users\\parko\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 2")
driver = webdriver.Chrome(service=service, options=options)
    driver.get("https://exmaplewebsitelogin.com")
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
    driver.add_cookie(cookie)

As I mentioned before, it doesn't work. the Chrome profile is not saving the cookies (user-data-dir=C..) and the cookies save I tried to do doesn't work either so it requires me to login again and again. Please help :)

Upvotes: 0

Views: 3334

Answers (1)

cruisepandey
cruisepandey

Reputation: 29362

See in Selenium you can get the current cookies (let's say first run) like this :-

current_cookies = driver.get_cookies()

current_cookies is a list in Selenium-Python bindings.

Now based on your requirement you can add the cookies like this :

driver.add_cookie(current_cookies[0])

or

driver.add_cookie(current_cookies[1])

A demo with Google.com below :-

driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://www.google.com")
wait = WebDriverWait(driver, 20)
current_cookies = driver.get_cookies()
for cook in current_cookies:
  print(cook)

driver.add_cookie(current_cookies[1])

Output :

{'domain': '.google.com', 'expiry': 1643970255, 'httpOnly': True, 'name': 'NID', 'path': '/', 'sameSite': 'None', 'secure': True, 'value': '220=sNWa6cL99uxrimg5YVSfFsWm2eSHAZ9nbGJHkx1PISRODhMQXEV15MAtpWM7E4uMtUlV8vw9lpxxQ5c3OO99-x4ZK72xh-jEWPdTWnIs8PcDuAzTwKyhJ4Bw2kRuxTyICygjdybV_FlPevuDrjlY6fzvbkBTaTcskVWASC31rp4'} {'domain': 'www.google.com', 'expiry': 1628180657, 'httpOnly': False, 'name': 'UULE', 'path': '/', 'secure': True, 'value': 'a+cm9sZTogMQpwcm9kdWNlcjogMTIKdGltZXN0YW1wOiAxNjI4MTU5MDU2NTY3MDAwCmxhdGxuZyB7CiAgbGF0aXR1ZGVfZTc6IDEyOTcxNTk4NwogIGxvbmdpdHVkZV9lNzogNzc1OTQ1NjI3Cn0KcmFkaXVzOiA2OTY3MjYyNDAKcHJvdmVuYW5jZTogNgo='} {'domain': '.google.com', 'expiry': 1630751056, 'httpOnly': False, 'name': '1P_JAR', 'path': '/', 'sameSite': 'None', 'secure': True, 'value': '2021-08-05-10'}

Process finished with exit code 0

Upvotes: 1

Related Questions