Selenium:将本地存储保存到文件或从文件加载 [英] Selenium: Save and Load LocalStorage to/from File

查看:926
本文介绍了Selenium:将本地存储保存到文件或从文件加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在用python编写脚本,以告诉我WhatsApp中有多少未读消息. 要获取未读邮件的数量,硒需要打开 web.whatsapp.com ,但是我每次都必须进行身份验证.我发现WhatsApp将数据保存在LocalStorage中进行身份验证,因此我试图弄清楚如何将LocalStorage中的内容保存到文件中,然后再从中读取并设置所有密钥.

I'm currently writing a script in python to tell me how many unread messages I have in WhatsApp. To get the count of unread messages selenium opens web.whatsapp.com however I have to authenticate every time. I found out that WhatsApp saves the data to authenticate in the LocalStorage so I'm trying to figure out how I can save the contents from LocalStorage to a file and then later read from it and set all the keys.

我尝试过:

localStorage = driver.execute_script('return window.localStorage;')
print(localStorage)

但是当我这样做时,运行脚本的终端就会崩溃.

but when I do that my terminal running the script just crashes.

推荐答案

在浏览器上创建一个新的用户配置文件,将其激活并使用新创建的配置文件登录到web.whatsapp.com,然后关闭浏览器.运行python脚本并使用新的配置文件启动网络驱动程序,您仍然应该登录,即:

Create a new user profile on your browser, activate it and login to web.whatsapp.com using the newly created profile, close the browser. Run the python script and initiate the webdriver using the new profile and you should still be logged in, i.e.:

下面的示例适用于Firefox和web.whatsapp.com,但是一般概念可以在其他浏览器和网站上使用.

The example below is for Firefox and web.whatsapp.com, but the general concept can be used on other browsers and websites.

1-在浏览器URL框中键入about:profiles,然后按Enter键. 2-点击Create a New Profile

1 - Type about:profiles on the browser url box an press enter
2 - Click Create a New Profile

3-为新的配置文件选择namefolder(请注意配置文件位置),在这种情况下:d:\ff_profiles\selenium_user

3 - Choose a name and folder for the new profile (take note of the profile location), in this case : d:\ff_profiles\selenium_user

4-激活新的浏览器配置文件

4 - Activate the new browser profile

5-登录到要跳过硒登录过程的任何网站,在本例中为web.whatsapp.com

5 - Login to any website that you want to skip the login process on selenium, in this case, web.whatsapp.com

6-成功登录后(在扫描QR码之后)关闭浏览器

6 - Once you've logged in successfully (after scanning the QR code) close the browser

7-在脚本上使用配置文件

7 - Using the profile on your script

from selenium import webdriver
fp = webdriver.FirefoxProfile('d:\\ff_profiles\\selenium_user') 
driver = webdriver.Firefox(firefox_profile=fp)
driver.get("https://web.whatsapp.com")
# you should still be logged in.

这篇关于Selenium:将本地存储保存到文件或从文件加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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