Python 和 Selenium - 重用 whatsapp 网络会话 [英] Python and Selenium - Reuse a whatsapp web session

查看:47
本文介绍了Python 和 Selenium - 重用 whatsapp 网络会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Chromedriver 上通过 Python 3 和 Selenium 重用 whatsapp 网络会话.

I want to reuse a whatsapp web session via Python 3 and Selenium on Chromedriver.

为此,我需要获取现有会话的本地存储并将其设置为新的浏览器实例,然后再次打开 whatsapp 网络.

For which I need to grab the localstorage of an existing session and set it to the new browser instance before opening whatsapp web again.

我是在登录会话中这样做的:

I do so, on a logged in session:

cacho="";
for (var i = 0; i < localStorage.length; i++){
cacho+="localStorage.setItem('"+localStorage.key(i)+"', '"+localStorage.getItem(localStorage.key(i))+"');"
}
alert(cacho)

使用 driver.execute_script(),我得到了工作的本地存储数据.

With driver.execute_script(), and I get the working localstorage data.

我复制了整个内容(几行 javascript),然后在打开一个新的浏览器实例时:

I copy the whole thing (a few javascript lines) and then, when opening a new browser instance, I do:

driver.execute_script(the_whole_thing)
driver.get(url)

但是会话不是活动的.

我在这里遗漏了什么?

推荐答案

这是您可以通过存储 cookie 重复使用 whatsapp 网络会话的方法.

this is how you can reuse a whatsapp web session by storing cookies.

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

chrome_options = Options()
chrome_options.add_argument("--user-data-dir=chrome-data")
driver = webdriver.Chrome('chromedriver.exe',options=chrome_options)
driver.get('https://web.whatsapp.com')
time.sleep(30)

这篇关于Python 和 Selenium - 重用 whatsapp 网络会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆