![](/img/trans.png)
[英]Chrome Extension Not Running in Selenium Javascript Webdriver Test
[英]Setting cookie with Chrome extension and Selenium Webdriver
當我使用 Selenium Webdriver 實例打開頁面時,我試圖使用 Chrome 擴展程序設置 cookie。 我嘗試遵循不同 Stack Overflow 帖子中建議的各種方法,但當我打開頁面檢查器時,它們都沒有顯示。
清單 - 我確保列出任何 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"]
}
]
}
后台 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);
});
}
內容腳本 - 我讀過您通常不應該嘗試從內容腳本中設置 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" });
});
所以這不是一個完整的答案,但我意識到無論你在 cookie 中放入什么 URL,它都會被歸檔到你正在運行擴展程序的頁面的 URL 下。 所以 chrome.cookies.set({ url: " https://google.com/ ", name: "CookieVar0", value: "123" }); 正在工作,但它不在“ https://google.com/ ”下,而是在“當前頁面網址”下。 在我更改格式以匹配一個設置 CookieVar0 之前,一個設置“Sample1”不起作用,然后就可以了。
所以我仍然無法讓它在任何地方都能按預期工作,但至少有一些進展。 希望這可以幫助某人!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.