我需要使用Selenium从扩展的通知中提取链接以测试许多网站。
我正在测试的扩展程序是github full code available here中的此示例,如果用户单击了Mozilla的任何网站中的链接,它将显示通知中的单击的链接。
background-script.js
/*
Log that we received the message.
Then display a notification. The notification contains the URL,
which we read from the message.
*/
function notify(message) {
console.log("background script received message");
var title = browser.i18n.getMessage("notificationTitle");
var content = browser.i18n.getMessage("notificationContent", message.url);
browser.notifications.create({
"type": "basic",
"iconUrl": browser.extension.getURL("icons/link-48.png"),
"title": title,
"message": content
});
}
/*
Assign `notify()` as a listener to messages from the content script.
*/
browser.runtime.onMessage.addListener(notify);
manifest.json:
{
"manifest_version": 2,
"name": "__MSG_extensionName__",
"description": "__MSG_extensionDescription__",
"version": "1.0",
"homepage_url": "https://github.com/mdn/webextensions-examples/tree/master/notify-link-clicks-i18n",
"icons": {
"48": "icons/link-48.png"
},
"permissions": ["notifications"],
"background": {
"scripts": ["background-script.js"]
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content-script.js"]
}
],
"default_locale": "en"
}
contentscript.js
/*
If the click was on a link, send a message to the background page.
The message contains the link's URL.
*/
function notifyExtension(e) {
var target = e.target;
while ((target.tagName != "A" || !target.href) && target.parentNode) {
target = target.parentNode;
}
if (target.tagName != "A")
return;
console.log("content script sending message");
browser.runtime.sendMessage({"url": target.href});
}
/*
Add notifyExtension() as a listener to click events.
*/
window.addEventListener("click", notifyExtension);
我想收集显示的通知文本(链接)。我将在Selenium中输入许多网站。
我的问题是:如何使用Selenium检查通知?当我右键单击通知时,没有得到类似于浏览器中的选项。因此我无法找到要与Selenium一起使用的元素名称。任何提示都会有所帮助。
答案 0 :(得分:0)
扩展名创建的通知属于操作系统,而不是浏览器。您将无法像普通的HTML元素一样检查它们。
您可以做的是监听browser.notifications.onClosed
或browser.notifications.onClicked
事件,并在扩展代码中获取该信息。
参考文献: