Python and Selenium - Reboot program and reuse same browser session

喜夏-厌秋 提交于 2021-02-10 15:13:03

问题


Scenario:

I am working an an auto whatsapp responder using whatsapp web.

I log in via chromedriver on selenium with python 3.

I run a function that does some stuff inside a while True.

Problem:

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.

Question:

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.

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.)

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.


回答1:


Whatsapp stores session in localStorage of the browser.

You can extract the localStorage and save to a file on closing of a session.

Upon instantiating a session check if this file exists, then parse the file and update localStorage with saved values before opening a URL.



来源:https://stackoverflow.com/questions/45413194/python-and-selenium-reboot-program-and-reuse-same-browser-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!