如何使用Selenium Python保存Whatsapp Web会话?

时间:2018-06-11 07:11:53

标签: python python-3.x selenium selenium-webdriver selenium-chromedriver

我的期望是在验证后会自动添加到cookeies数组中,但事实并非如此。

driver = webdriver.Chrome(chromedriver)
whatsapp_url = "https://web.whatsapp.com"
driver.get(whatsapp_url)
print(driver.get_cookies())
time.sleep(30) # We are doing the manual QR code verification here
print(driver.get_cookies())

driver.get_cookies()之前和之后都是空的

3 个答案:

答案 0 :(得分:2)

我无法评论Abhi Bhalgami的回答,但我需要根据另一篇文章进行一些更改:

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

chromepath = r'E:\chromedriver\chromedriver.exe'
options = webdriver.ChromeOptions() 
options.add_argument("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data")
driver = webdriver.Chrome(executable_path=chromepath, chrome_options=options)

extract and save whatsapp session from chrome browser local storage and then use it on another device

答案 1 :(得分:1)

我不确定,但您可以使用 pickle 保存当前的 cookie,例如:

import pickle
from selenium import webdriver

driver = webdriver.Chrome()
driver.get("http://www.google.com")
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))

然后使用此代码将它们添加回来:

import pickle
from selenium import webdriver

driver = webdriver.Chrome()
driver.get("http://www.google.com")
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
    driver.add_cookie(cookie)

答案 2 :(得分:0)

Web Whatsapp 在 IndexedDB 中存储会话并将这些值同步到本地存储。

这是您可以尝试的工作代码:tithiwa/session.py 使用此 javascript code