我正在使用Java中的Selenium开发自动化测试。我想删除本地和会话存储,Cookie等,以使“ WebDriver”清晰可见。我需要它从站点注销并为所有测试使用相同的WebDriver,而不是为每个测试用例关闭和打开浏览器。
如图所示,当我删除整个本地存储时,它将被删除。然后,我重新加载页面。我没有注销,所有本地存储项目再次出现。
然后我使用键盘上的“删除”键仅删除active_user_session项目。然后我立即发现自己已注销。
这是一件奇怪的事情...为什么当我删除所有存储时,我没有注销,并且在重新加载页面时恢复了信息。但是,当我仅删除一个active_user_session项时,我被立即注销了??
好的。让我们看一下代码。
((JavascriptExecutor)(driver)).executeScript( String.format(
"window.localStorage.removeItem('%s');", "access_token"
));
这个很好用。但是我似乎需要删除active_user_session项目...
((JavascriptExecutor)(driver)).executeScript( String.format(
"window.localStorage.removeItem('%s');", "active_user_session"
));
此代码不起作用:active_user_session仍然存在!
更有趣的事情:
((JavascriptExecutor)(driver)).executeScript(
"window.localStorage.clear();"
);
删除除active_user_session项以外的所有项!
为什么???你能解释一下吗?可能我不知道... 谢谢!
答案 0 :(得分:0)
答案 1 :(得分:0)
试试这个 - 会以老式的方式清除浏览器中的所有内容 - 但否则会完全自动化(即 cookie、登录等都将再次被请求)...
#https://stackoverflow.com/questions/54404721/can-not-delete-item-from-local-storage-in-browser/54415520#54415520
import time, os
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import JavascriptException
def new_profile():
global ev, parent, d, w
global path_core, path_exec
global ev
path_core= os.path.join(os.path.expanduser("~"), 'Folder_Path_to_your_Code')
path_exec = os.path.join(path_core, "chromedriver.exe") #assumes chromedriver.exe saved in same location as your code...
d = webdriver.Chrome(executable_path=path_exec)
[ev] = [d.execute_script]
parent = d.current_window_handle
w = WebDriverWait(d,5)
#d.implicitly_wait(0.5)
print('new profile complete')
def browser_clear(url=''):
global start, ss, w
[ss] = [time.sleep]
w_old, w = w, WebDriverWait(d, 20)
url = d.current_url if url == ''else url
script0 = '''document.querySelector('body').querySelector('settings-ui')["shadowRoot"].querySelector('settings-main')['shadowRoot'].querySelector('settings-basic-page')['shadowRoot'].querySelector('settings-privacy-page')['shadowRoot'].querySelector('#pages').querySelector('#clearBrowsingData')['shadowRoot'].lastElementChild.click()'''
script1 = '''document.querySelector('body').querySelector('settings-ui')["shadowRoot"].querySelector('settings-main')['shadowRoot'].querySelector('settings-basic-page')['shadowRoot'].querySelector('settings-privacy-page')['shadowRoot'].querySelector('settings-clear-browsing-data-dialog')['shadowRoot'].querySelector('iron-pages').querySelector('#basic-tab').querySelector('settings-dropdown-menu')['shadowRoot'].lastElementChild.selectedIndex = 4;'''
script2 = '''document.querySelector('body').querySelector('settings-ui')["shadowRoot"].querySelector('settings-main')['shadowRoot'].querySelector('settings-basic-page')['shadowRoot'].querySelector('settings-privacy-page')['shadowRoot'].querySelector('settings-clear-browsing-data-dialog')['shadowRoot'].querySelector('#clearBrowsingDataDialog').children[3].lastElementChild.parentElement.children[2].click()'''
script_wait = '''{if (document.querySelector('body').querySelector('settings-ui')["shadowRoot"].querySelector('settings-main')['shadowRoot'].querySelector('settings-basic-page')['shadowRoot'].querySelector('settings-privacy-page')['shadowRoot'].querySelector('settings-clear-browsing-data-dialog')) {return true} else {return false}}'''
d.get('chrome://settings/')
ev(script0)
start = time.time()
while (time.time() - start <= 30) and ev(script_wait) == False: ss(0.5)
ss(1)
ev(script1), ss(0.5), ev(script2)
start = time.time()
while (time.time() - start <= 180) and ev(script_wait) == True: ss(0.5)
try:
d.get(url)
except TimeoutError:
w.until(lambda x: x.execute_script("return document.readyState") == "complete")
w = w_old
d.get(url)
if __name__ == '__main__':
new_profile()
browser_clear()