Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save the session of Whatsapp web with Selenium Python?

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

like image 438
ishandutta2007 Avatar asked Jun 11 '18 07:06

ishandutta2007


Video Answer


2 Answers

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.

like image 171
Abhi Avatar answered Nov 14 '22 15:11

Abhi


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

like image 26
Leonardo Wolter Avatar answered Nov 14 '22 14:11

Leonardo Wolter