[英]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.