derpascal
derpascal

Reputation: 162

how do I acces whatsapp web with selenium in python?

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 ?

Upvotes: 0

Views: 1572

Answers (2)

Steve de Peijper
Steve de Peijper

Reputation: 56

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.

Upvotes: 1

kennysliding
kennysliding

Reputation: 2977

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)

Upvotes: 3

Related Questions