0

So I am using geckodriver.exe (for Firefox), and I use the following code to acces whatsapp web:

from selenium import webdriver
browser = None
def init():
    browser = webdriver.Firefox(executable_path=r"C:/Users/Pascal/Desktop/geckodriver.exe")
    browser.get("https://web.whatsapp.com/")
init()

But everytime I rerun the code, the QR-Code from whatsappweb has to be scanned again and I dont want that. In my normal chrome browser I dont have to scan the QR-Code everytime. How can I fix this ?

2 Answers 2

3

Since every time you close your selenium driver/browser, the cookies that attached with the session will also be deleted. So to restore the cookies you haved saved, you can retrieve it after the end of the session and restore it in the beginning of the next.

For getting the cookies,

# Go to the correct domain, i.e. your Whatsapp web
browser.get("https://www.example.com")
# get all the cookies from this domain
cookies = browser.get_cookies()
# store it somewhere, maybe a text file

For restoring the cookies

# Go to the correct domain, i.e. your Whatsapp web
browser.get("https://www.example.com")
# get back the cookies
cookies = {‘name’ : ‘foo’, ‘value’ : ‘bar’}
browser.add_cookies(cookies)
Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately it did not work. There were no cookies...
1

What you could do is define a profile in Firefox. Then open firefox with that profile and open web.whatsapp.com. You will be prompted with the QR code. You link that instance. From there you can use the newly created profile in Python.

Creating a new profile can be done by typing about:profiles in the url section of Firefox: enter image description here Then open the browser by clicking 'Launch profile in new browser': enter image description here

In your Python code you create a reference to this profile:

options.add_argument('-profile')
options.add_argument('/home/odroid/Documents/PythonProfile')

A step by step guide can also be found here.

1 Comment

It works perfectly, and saves the session started with a site

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.