简体   繁体   中英

Setting cookie with Chrome extension and Selenium Webdriver

I am trying to use a Chrome extension to set a cookie when I get a Selenium Webdriver instance to open a page. I have tried following a variety of ways suggested in different Stack Overflow posts but none of them ever show when I open the page inspector.

Manifest - I made sure to list permissions to any URL:

{
    "name": "Test",
    "description": "Test",
    "version": "2.0",
    "manifest_version": 2,
    "permissions": ["cookies", "<all_urls>", "background", "tabs", "webRequest", "webRequestBlocking", "http://*/*"],    
    "background": {
        "scripts": ["background.js"],
        "persistent": true
    } ,
    "content_scripts": [
      {
        "matches": ["*://*/*"],
        "js": ["script.js"]          
      }
    ] 
}

Background JS - I have several attempts to create a cookie and none of them are showing when I inspect the web or background page:

chrome.cookies.set({ url: "https://google.com/", name: "CookieVar0", value: "123" });

chrome.webRequest.onBeforeRequest.addListener(
        function(details) {

        chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
          chrome.tabs.sendMessage(tabs[0].id, {test: testVar}, function(response) {
            console.log("Response connected");
            setCookie();
            chrome.cookies.set({ url: "http://example.google.com", name: "CookieVar1", value: "123" });
          });
        });

        }, 
        ...

function setCookie(){
    console.log("Set Cookie");

    chrome.cookies.set({"name":"Sample1","url":"http://developer.chrome.com/extensions/cookies.html","value":"Dummy Data"},function (cookie){
        console.log(JSON.stringify(cookie));
        console.log(chrome.extension.lastError);
        console.log(chrome.runtime.lastError);
    });

}

Content Script - I've read that you're normally not supposed to try to set cookies form the content script but some posts mentioned it so I tried it anyways:

window.addEventListener('load', loadEvent => {
    console.log("Test 321");
    // window.customlog = "Updated window!!!";
    let window = loadEvent.currentTarget;
    window.document.title='You changed me!';
    chrome.cookies.set({ url: "http://example.google.com", name: "CookieVar3", value: "123" });
});

So its not a complete answer but something I realized was that no matter what URL you put in the cookie it is going to be filed under the url of the page you are running the extension. So chrome.cookies.set({ url: " https://google.com/ ", name: "CookieVar0", value: "123" }); was working but it wasn't under " https://google.com/ " but "current page url" instead. The one setting "Sample1" was not working until I changed the format to match the one setting CookieVar0 and then it was fine.

So I still can't get it to work as desired everywhere but there is at least some progress. Hope this helps someone!

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