简体   繁体   中英

How can I get local storage data from chrome using selenium for java?

I have a chrome extension that stores local data with a manifest of

{
    "name": "Tab Listener",
    "version": "1.0",
    "manifest_version": 2,
    "permissions": ["storage"],
    "background": {
        "scripts": ["content.js"]
    }
}

and a content.js of

chrome.tabs.onActivated.addListener(function(activeInfo) {
    chrome.storage.local.set({currentTabIndex: 'testValue'}, function() {
    });

    chrome.storage.local.get('currentTabIndex', function(result) {
    alert('Value currently is ' + result.currentTabIndex);
    });
});


To the best of my knowledge this stores the variable fine, and the alert is displayed. Then, I try to get this variable in a java application using

WebStorage webStorage = (WebStorage) new Augmenter().augment(driver);
LocalStorage localStorage = webStorage.getLocalStorage();
SessionStorage sessionStorage = webStorage.getSessionStorage();
System.out.println(localStorage.getItem("currentTabIndex"));
System.out.println(sessionStorage.getItem("currentTabIndex"));

and both cases return null. I do launch my driver with chrome settings

ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:\\AutoSig\\personal\\chrome");
driver = new ChromeDriver(options);

and confirmed that this indeed does store the data for chrome and carries over session to session. I'm relatively new to chrome add ons, and not sure what I'm doing wrong. Additonally, I have tried using the utility classes suggested in This StackOverflow Post and have had no luck. Any assistance would be greatly appreciated.

public static String getItemFromLocalStorage(String key)
{
        JavascriptExecutor js = ((JavascriptExecutor)ios_mob_driver);
        return (String) js.executeScript(String.format("return window.localStorage.getItem('%s');", key));
}

I used and working fine for me .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM