问题描述
场景:
我正在使用 whatsapp web 工作一个自动 whatsapp 响应器.
I am working an an auto whatsapp responder using whatsapp web.
我使用 python 3 在 selenium 上通过 chromedriver 登录.
I log in via chromedriver on selenium with python 3.
我运行了一个函数,它在 while True
中做一些事情.
I run a function that does some stuff inside a while True
.
问题:
有时,由于缺乏与手机的连接,或其他任何问题,程序只是无法以正确的方式运行.
Sometimes, due to a lack of conectivity with the phone, or whatever other problems, the program just does not keep running the right way.
有很多因素可能会导致整个事情失去正确的流程.我正在分析它们并尽我所能修复它们.
There are a lot of factors that might cause the whole thing to lose the right flow. I am analyzing them all and fixing them as best as I can.
问题:
我想出了一个想法,也许如果我每小时(或每千次迭代)重新启动整个过程,它会变得更加可靠.因为无论发生什么它都会重新找到流程,如果我还没有发现错误.
I came up with the idea that maybe if I restart the whole thing every hour (or every whatever-thousands iterations) it would become more solid. As it will refind the flow no matter what happens, if I did not catch the bug yet.
是否可以在不丢失浏览器会话的情况下重新启动整个过程?Whatsapp web 需要 QR 扫描,但它允许在进一步的连接中保持会话活动"(我真的不知道它是如何工作的......如果是 cookie 或其他东西.)
Is it possible to restart the whole thing, without losing the browser session? Whatsapp web requires a QR scan, but it allows a "keep session alive in further connections" (which I do not really know how it works... if cookies or something else.)
注意:我知道 python 脚本可以重新启动,但这里更大的问题是重用浏览器会话.当然,我正在做我的研究.到目前为止,我读到的所有内容都没有让我找到一个可靠的解决方案,这就是为什么我向所有超级酷的大脑询问.
Note: I know that a python script can be rebooted, but the bigger problem here is to reuse the browser session. Of course I am doing my research. None of what I read so far made me come with a solid solution, and that is why I ask to all the super cool brains out there.
推荐答案
Whatsapp 将会话存储在浏览器的 localStorage
中.
Whatsapp stores session in localStorage
of the browser.
您可以提取localStorage并保存到文件在会议结束时.
You can extract the localStorage and save to a file on closing of a session.
在实例化会话时检查此文件是否存在,然后在打开 URL 之前解析该文件并使用保存的值更新 localStorage.
Upon instantiating a session check if this file exists, then parse the file and update localStorage with saved values before opening a URL.
这篇关于Python 和 Selenium - 重新启动程序并重用相同的浏览器会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!