7

My expectation was something would get added to the cookeies array automatically after validation but that isn't the case.

driver = webdriver.Chrome(chromedriver)
whatsapp_url = "https://web.whatsapp.com"
driver.get(whatsapp_url)
print(driver.get_cookies())
time.sleep(30) # We are doing the manual QR code verification here
print(driver.get_cookies())

driver.get_cookies() is empty before and after as well

4
  • you can use pickle Commented Jun 11, 2018 at 16:52
  • I am aware of that( stackoverflow.com/a/15058521/865220) just for QR code none of my experimentations are working. Can you give the full working code for saving QR code of whatsapp. Commented Jun 11, 2018 at 17:11
  • 1
    you can save whole session with chrome_options.add_argument("user-data-dir=" + os.path.dirname(sys.argv[0])) browser = webdriver.Chrome(chrome_options=chrome_options) Commented Jun 11, 2018 at 17:21
  • 1
    full working code in answer please, very difficult to understand from 1-2 lines in comment. Commented Jun 11, 2018 at 17:28

5 Answers 5

16

Try this:

options = webdriver.ChromeOptions();
options.add_argument('--user-data-dir=./User_Data')
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://web.whatsapp.com/')

You need to login first time. It'll work always when you have to store your session. No need to import/export cookies.

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

1 Comment

I tried this method. However, it doesn't work. QR code is still needed to login
4

I couldn't comment Abhi Bhalgami answer but I need to made some changes based on another post:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chromepath = r'E:\chromedriver\chromedriver.exe'
options = webdriver.ChromeOptions() 
options.add_argument("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data")
driver = webdriver.Chrome(executable_path=chromepath, chrome_options=options)

extract and save whatsapp session from chrome browser local storage and then use it on another device

Comments

2

https://pt.stackoverflow.com/questions/241604/salva-o-estado-ou-os-cookies-do-navegador-utilizando-o-selenium

This worked fine for me! I

import os
from selenium import webdriver

dir_path = os.getcwd()
profile = os.path.join(dir_path, "profile", "wpp")
options = webdriver.ChromeOptions()
options.add_argument(
    r"user-data-dir={}".format(profile))

browser = webdriver.Chrome("./chromedriver.exe", chrome_options=options)

browser.get("https://web.whatsapp.com")

Comments

0

am not sure but You can save your current cookies using pickle, For example:

import pickle
from selenium import webdriver

driver = webdriver.Chrome()
driver.get("http://www.google.com")
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))

after that add them back using this code:

import pickle
from selenium import webdriver

driver = webdriver.Chrome()
driver.get("http://www.google.com")
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
    driver.add_cookie(cookie)

Comments

0

Web Whatsapp stores sessions in IndexedDB and sync those values to local storage.

Here is the working code you can try: tithiwa/session.py by using this javascript code

1 Comment

is this still working Sir? i've tried several times but with no luck.. could you please show the correct way?

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.