简体   繁体   中英

Using selenium: How to keep logged in after closing Driver in Python in whatsapp

I don't want to log in over and over again in https://web.whatsapp.com . I've tried some solutions but it's not working using selenium chrome driver.

options=Options
options.add_argument("user-data-dir=C:\\Users\\oyo\AppData\\Local\\Google\\Chrome\\User Data")
browser = webdriver.Chrome("chrome_options=options")

TypeError: add_argument() missing 1 required positional argument: 'argument'

I was able to save the session by adding a start-option to chrome -> You need to add the option --user-data-dir <folder> .

I've used code from these places.

I'm running this code under Ubuntu 18.04.

Close google-chrome before runnning this code. Else selenium will re-use the current browser instance and won't be able to run it with the --user-data-dir <folder> -option.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

# Replace below path with the absolute path
# to chromedriver in your computer

options = webdriver.ChromeOptions();
options.add_argument("user-data-dir=~/.chrome_driver_session")
# Create the folder. Change path accordingly

driver = webdriver.Chrome('./chromedriver_78/chromedriver', chrome_options=options)
driver.get("https://web.whatsapp.com/")
wait = WebDriverWait(driver, 600)

# Replace 'Friend Name' with the name of your friend or the name of a group
target = '"Friend Name"'

# Replace the below string with your own message
# I'm unsure why it needs two empty spaces in front of it.

string = "  " + "Nachricht von Wichtel_Whatsapp"

x_arg = '//span[contains(@title,' + target + ')]'
group_title = wait.until(EC.presence_of_element_located((By.XPATH, x_arg)))
group_title.click()

print("Clicked")

default_input = "Schreib eine Nachricht"
# Change the Text with the default of the input-field

inp_xpath = "//div[contains(.,'" + default_input + "')]"
input_box = wait.until(EC.presence_of_element_located((By.XPATH, inp_xpath))).find_element_by_xpath('..')
input_box.send_keys(string)
# If the Text is written in the input field, use this line:
# input_box.send_keys(string + Keys.ENTER)

In order to transfer your session from one browser instance to another all you need to do is to copy Cookies from the first session to second. Selenium provides a variety of methods allowing cookies manipulation , you will need:

  1. driver.get_cookies() - to fetch the cookies from the session where you're logged in
  2. add_cookie() - to restore cookies into the new browser instance

In your case you can store the cookies into an interim file as the last step of the first execution and read them from the file as the first step of the second execution.

Example code:

#Store cookies
cookies = driver.get_cookies()
for cookie in cookies:
    with open('cookies.txt', 'a') as stored_cookies:
        stored_cookies.write(str(cookie) + '\n')

#Restore cookies
with open('cookies.txt') as stored_cookies:
    cookie = eval(stored_cookies.readline())
    driver.add_cookie(cookie)
import os
from selenium import webdriver

dir_path = os.getcwd()
profile = os.path.join(dir_path, "profile", "wpp")
options = webdriver.ChromeOptions()
options.add_argument(
        r"user-data-dir={}".format(profile))

browser = webdriver.Chrome("./chromedriver.exe", chrome_options=options)

browser.get("https://web.whatsapp.com")

I think this is the best way

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM