How do I save a whatsapp web session in selenium?

Viewed 1933

I am trying to acces whatsapp web with python without having to scan the QR code everytime I restart the program (because in my normal browser I also dont have to do that). But how can I do that? Where is the data stored that tells whatsapp web to connect to my phone? And how do I save this data and send it to the browser when I rerun the code?

I already tried this because someone told me I should save the cookies:

from selenium import webdriver
import time
browser = None
cookies = None
def init():
    browser = webdriver.Firefox(executable_path=r"C:/Users/Pascal/Desktop/geckodriver.exe")
    browser.get("https://web.whatsapp.com/")
    time.sleep(5) # in this time I scanned the QR to see if there are cookies 
    cookies = browser.get_cookies()
    print(len(cookies))
    print(cookies)
init()

Unfortunately there were no cookies.. The output was 0 and []. How do I fix this probblem?

2 Answers

As mentioned in the answer to this question, pass your Chrome profile to the Chromedriver in order to avoid this problem. You can do it like this:

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

options = webdriver.ChromeOptions() 
options.add_argument("user-data-dir=C:\\Path") #Path to your chrome profile
driver = webdriver.Chrome(executable_path="C:\\Users\\chromedriver.exe", options=options)

This one works for me, I just created a folder, on the home directory of the script and a little modifications and it works perfectly.

###########

E_PROFILE_PATH = "user-data-dir=C:\Users\Denoh\Documents\Project\WhatBOts\SessionSaver"

################## This is the Config File that I will import later ##################

The main script starts here ##################

from selenium import webdriver from config import E_PROFILE_PATH

options = webdriver.ChromeOptions() options.add_argument(E_PROFILE_PATH)

driver = webdriver.Chrome(executable_path='chromedriver_win32_86.0.4240.22\chromedriver.exe', options=options) driver.get('https://web.whatsapp.com/')

Related