5

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

ishandutta2007
  • 16,676
  • 16
  • 93
  • 129
  • you can use pickle – Prany Jun 11 '18 at 16:52
  • I am aware of that( https://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. – ishandutta2007 Jun 11 '18 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) – Prany Jun 11 '18 at 17:21
  • 1
    full working code in answer please, very difficult to understand from 1-2 lines in comment. – ishandutta2007 Jun 11 '18 at 17:28

5 Answers5

14

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.

Abhi
  • 182
  • 2
  • 7
  • I tried this method. However, it doesn't work. QR code is still needed to login –  Feb 05 '21 at 05:17
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

Leonardo Wolter
  • 131
  • 1
  • 4
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")
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)
Zakaria Ferzazi
  • 364
  • 3
  • 4
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

Navpreet Devpuri
  • 503
  • 4
  • 19
  • is this still working Sir? i've tried several times but with no luck.. could you please show the correct way? – Mr Noob Aug 05 '22 at 15:58