2

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?

derpascal
  • 172
  • 1
  • 10

2 Answers2

3

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)
Sushil
  • 5,440
  • 1
  • 8
  • 26
  • I tried it. It goes to whatsapp web and then the program crashes with error code: Could not open pak file for writing: Process could not acces the file, because its already in use of another process. (0x20) – derpascal Oct 16 '20 at 11:45
  • It is because you might have another chrome/firefox open. Close that and then run the script. – Sushil Oct 16 '20 at 11:48
  • If I have already a browser opened it throws this exception: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir but when I dont have a browser already opened it throws the error that I mentioned above – derpascal Oct 16 '20 at 11:53
  • Maybe the browser still runs in the background. Just end task the browser and try running the script. – Sushil Oct 16 '20 at 11:57
  • I saw nothing running in the background. I tried with Firefox and there were no errors, but the QR-Code still had to be scanned but I changed user-data-dir to my firefox profile path . – derpascal Oct 16 '20 at 12:05
  • Oh...If this doesn't work, then maybe u can try adding cookies. – Sushil Oct 16 '20 at 12:09
  • But there are no cookies... If you want try it yourself because unfortunately I dont know how to analyse such problems :/ – derpascal Oct 16 '20 at 12:14
0

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/')