如何在 Selenium 中保存 Whatsapp 网络会话?

der*_*cal 3 python selenium

我试图用 python 访问whatsapp web,而不必每次重新启动程序时扫描二维码(因为在我的普通浏览器中我也不必这样做)。但我怎样才能做到这一点呢?告诉 Whatsapp Web 连接到我的手机的数据存储在哪里?当我重新运行代码时,如何保存这些数据并将其发送到浏览器?

我已经尝试过这个,因为有人告诉我我应该保存cookie:

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()
Run Code Online (Sandbox Code Playgroud)

不幸的是没有cookie..输出是0和[]。我该如何解决这个问题?

Sus*_*hil 5

正如此问题的答案中所述,请将您的Chrome个人资料传递给Chromedriver以避免此问题。你可以这样做:

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)
Run Code Online (Sandbox Code Playgroud)