Selenium Webdriver - Setting Cookie With Chrome Extension

141
June 15, 2018, at 9:20 PM

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 stackoverflow posts but none of them ever show when I open the page inspector. Any pointers would be greatly appreciated.

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" });
});
Answer 1

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!

READ ALSO
Jest testing a function with no return [on hold]

Jest testing a function with no return [on hold]

I am in the process of unit testing a function using Jest that merely passes on its parameters to another function by calling it and has no return

93
How to get html + js from url after page load completely

How to get html + js from url after page load completely

I'm trying to get the HTML/JS from a URL after the page has fully loaded

110
adaptive main column width around sidebar

adaptive main column width around sidebar

I'm working on a custom stylesheet, therefore I have no way of editing the HTML itselfThe idea is quite simple

54
Fixed height and width only for desktop

Fixed height and width only for desktop

I am trying to implement a Vimeo video however for it to show correctly on mobile etc I have to have it in the responsive share mode however on desktop/notebook etc we want to have it a fixed height and width- how can I do this?

68