简体   繁体   English

使用 Chrome 扩展和 Selenium Webdriver 设置 cookie

[英]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.当我使用 Selenium Webdriver 实例打开页面时,我试图使用 Chrome 扩展程序设置 cookie。 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.我尝试遵循不同 Stack Overflow 帖子中建议的各种方法,但当我打开页面检查器时,它们都没有显示。

Manifest - I made sure to list permissions to any URL:清单 - 我确保列出任何 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:后台 JS - 我多次尝试创建 cookie,但在检查网页或后台页面时均未显示:

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:内容脚本 - 我读过您通常不应该尝试从内容脚本中设置 cookie,但有些帖子提到了它,所以我还是尝试了:

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.所以这不是一个完整的答案,但我意识到无论你在 cookie 中放入什么 URL,它都会被归档到你正在运行扩展程序的页面的 URL 下。 So chrome.cookies.set({ url: " https://google.com/ ", name: "CookieVar0", value: "123" });所以 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.正在工作,但它不在“ https://google.com/ ”下,而是在“当前页面网址”下。 The one setting "Sample1" was not working until I changed the format to match the one setting CookieVar0 and then it was fine.在我更改格式以匹配一个设置 CookieVar0 之前,一个设置“Sample1”不起作用,然后就可以了。

So I still can't get it to work as desired everywhere but there is at least some progress.所以我仍然无法让它在任何地方都能按预期工作,但至少有一些进展。 Hope this helps someone!希望这可以帮助某人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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