localStorage:
主要是用来作为本地存储来使用的,解决了cookie存储空间不足的问题(cookie中每条cookie的存储空间为4k),localStorage中一般浏览器支持的是5M大小,这个在不同的浏览器中localStorage会有所不同。
特性:
特性 | Cookies | LocalSrorage |
---|---|---|
存储生命周期 | 生成时就会指定一个maxAge值(cookies生存周期),周期内有效,默认关闭浏览器 | 除非数据被清除,否则一直存在 |
存放数据大小 | 4K左右 | 一般5M或更大 |
与服务器通信 | 来自于服务器,每次都存在http头里,保存过多数据可能会有性能问题 | 只在请求时使用数据,不参与和服务器的通信 |
易用性 | 使用setCookie,getCookie | 可以使用原生接口,也可在此封装对object和array有更好的支持 |
共同点 | 都保存在浏览器,和服务器端的session机制不同 |
存储位置:
使用方法:
get_local_storage.py
- 登陆浏览器,扫码获取token
2.js获取localstorage数据
3.存入data_token.yaml文件
def get_config_data():
filepath = YamlUtils().get_project_path("page/ui_config.yaml")
return YamlUtils().yaml_read(filepath)
class GetLocalStorage(BasePage):
def local_storage(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(10)
self.driver.get(self.driver.get(f"{self.base_url}/{self.orgid}/{self.groupid}/chat"))
time.sleep(20)
user_data = self.driver.execute_script("return window.localStorage.getItem(arguments[0]);", "user")
a = self.yaml_utils.get_project_path("page/data_token.yaml")
self.yaml_utils.yaml_open(a, "w", user_data)
yanl_utils.py
- yaml获取项目路径,文件读取加载等相关操作
class YamlUtils:
def get_project_path(self, path_a=""):
return os.path.dirname(os.path.abspath(os.path.dirname(__file__))) + "/" + path_a
def yaml_open(self, filepath, typ, data_w=""):
with open(filepath, typ, encoding="utf-8") as f:
if typ == "r":
data = yaml.load(f, Loader=yaml.FullLoader)
elif typ == "w":
data = yaml.dump(data_w, f, default_flow_style=False, encoding='utf-8', allow_unicode=True)
if typ in ["w", "r"]:
return data
def yaml_read(self, filePath):
return self.yaml_open(filePath, "r")
def yaml_load_data(self, filename):
with open(self.get_project_path(filename), encoding="utf-8") as f:
data = yaml.safe_load(f)
return data
base_page.py
- po思想
def get_config_data():
filepath = YamlUtils().get_project_path("page/ui_config.yaml")
return YamlUtils().yaml_read(filepath)
class BasePage:
_base_url = ""
def __init__(self, driver: WebDriver = None):
data = get_config_data()
self.yaml_utils = YamlUtils()
self.base_url = data['env'][data['default']]
self.orgid = data['orgid']
self.groupid = data['groupid']
if driver is None:
options = Options()
options.add_experimental_option('excludeSwitches', ['enable-automation'])
self.driver = webdriver.Chrome(options=options)
self.driver.maximize_window()
self.driver.implicitly_wait(10)
self.get_url("/chat")
self.__localstorage_login()
else:
self.driver = driver
if self._base_url != "":
self.get_url(self._base_url)
def __localstorage_login(self):
token_data = self.yaml_utils.yaml_load_data("page/data_token.yaml")
self.driver.execute_script("localStorage.setItem(arguments[0],arguments[1]);", 'user', token_data)
self.get_url("/chat")
self.driver_reference()
Last:
思路与获取cookies思路类似,报错信息后期补充更新