读取HTML5的localStorahe和sessionStorage的内容,并删除存储的内容。
#!usr/bin/env python #-*- coding:utf-8 -*- #操作HTML5存储对象 from selenium import webdriver import unittest,time class Html5Storage(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() def test_Html5localStorage(self): localStorageUrl = ‘http://www.w3school.com.cn/tiy/loadtext.asp?f=html5_webstorage_local‘ self.driver.get(localStorageUrl) time.sleep(2) #通过JavaScript语句,获取存储在localStorage中的lastname的值 lastName = self.driver.execute_script(‘return localStorage.lastname‘) print(‘lastName:‘,lastName) self.assertEqual(‘Gates‘,lastName) #通过JavaScript语句‘localStorage.clear();‘清除所有存储在localStorage中的存储值 self.driver.execute_script(‘localStorage.clear();‘) #清除存储在localStorage中的存储值后再次查看lastname的值 last_Name = self.driver.execute_script(‘return localStorage.lastname‘) self.assertEqual(None,last_Name) def test_Html5SessionStorage(self): sessionStorageUrl = "http://www.w3school.com.cn/tiy/loadtext.asp?f=html5_webstorage_session" self.driver.get(sessionStorageUrl) time.sleep(2) self.driver.find_element_by_tag_name(‘button‘).click() time.sleep(2) clickCpunt = self.driver.execute_script(‘return sessionStorage.clickcount‘) print(‘clickCpunt:‘,clickCpunt) self.assertEquals(1,int(clickCpunt)) self.driver.execute_script(‘sessionStorage.clear();‘) click_count = self.driver.execute_script(‘return sessionStorage.clickcount‘) self.assertEqual(None,click_count) def tearDown(self): self.driver.quit() if __name__ == ‘__main__‘: unittest.main()
原文地址:https://www.cnblogs.com/sleeping-cat/p/8278753.html
时间: 2024-11-05 21:31:50