Python 中用 Selenium 读写 Local Storage
约 90 个字 76 行代码 预计阅读时间 1 分钟
基础操作
写入
来源:selenium自动化测试通过localstorage绕过登陆
driver . execute_script ( 'localStorage.setItem("key", arguments[0]);' , val )
获取
来源:Selenium:将LocalStorage保存到文件或从文件加载 - IT屋-程序员软件开发技术分享社区
localStorage = driver . execute_script ( 'return window.localStorage;' )
封装为类
来源:如何使用Python和Selenium WebDriver获取localStorage - Thinbug
创建如下的类:
class LocalStorage :
def __init__ ( self , driver ):
self . driver = driver
def __len__ ( self ):
return self . driver . execute_script ( "return window.localStorage.length;" )
def items ( self ):
return self . driver . execute_script (
"var ls = window.localStorage, items = {} ; "
"for (var i = 0, k; i < ls.length; ++i) "
" items[k = ls.key(i)] = ls.getItem(k); "
"return items; " )
def keys ( self ):
return self . driver . execute_script (
"var ls = window.localStorage, keys = []; "
"for (var i = 0; i < ls.length; ++i) "
" keys[i] = ls.key(i); "
"return keys; " )
def get ( self , key ):
return self . driver . execute_script ( "return window.localStorage.getItem(arguments[0]);" , key )
def set ( self , key , value ):
self . driver . execute_script ( "window.localStorage.setItem(arguments[0], arguments[1]);" , key , value )
def has ( self , key ):
return key in self . keys ()
def remove ( self , key ):
self . driver . execute_script ( "window.localStorage.removeItem(arguments[0]);" , key )
def clear ( self ):
self . driver . execute_script ( "window.localStorage.clear();" )
def __getitem__ ( self , key ):
value = self . get ( key )
if value is None :
raise KeyError ( key )
return value
def __setitem__ ( self , key , value ):
self . set ( key , value )
def __contains__ ( self , key ):
return key in self . keys ()
def __iter__ ( self ):
return self . items () . __iter__ ()
def __repr__ ( self ):
return self . items () . __str__ ()
用法示例:
# get the local storage
storage = LocalStorage ( driver )
# set an item
storage [ "mykey" ] = 1234
storage . set ( "mykey2" , 5678 )
# get an item
print ( storage [ "mykey" ]) # raises a KeyError if the key is missing
print ( storage . get ( "mykey" )) # returns None if the key is missing
# delete an item
storage . remove ( "mykey" )
# iterate items
for key , value in storage . items ():
print ( " %s : %s " % ( key , value ))
# delete items
storage . clear ()