【问题标题】:How to inspect a Firefox extension's element using Selenium如何使用 Selenium 检查 Firefox 扩展的元素
【发布时间】:2019-07-09 12:22:18
【问题描述】:

我需要从使用 Selenium 的扩展通知中提取一个链接来测试许多网站。

我正在测试的扩展是 github full code available here 中的这个示例,如果用户点击了 Mozilla 的任何网站中的链接,它会在通知中显示点击的链接。

背景脚本.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 一起使用的元素名称。任何提示都会有所帮助。

【问题讨论】:

    标签: selenium firefox firefox-addon firefox-addon-webextensions


    【解决方案1】:

    扩展程序创建的通知属于操作系统,而不是浏览器。您将无法像普通 HTML 元素一样检查它们。

    您可以做的是监听browser.notifications.onClosedbrowser.notifications.onClicked 事件并在扩展代码中获取该信息。

    参考资料:

    【讨论】:

    • 我使用的是 Firefox 扩展而不是 Chrome
    • 这是一回事,因为 Firefox WebExtensions 实现了 chrome 命名空间。我编辑了我的答案。
    • 好的。但是,如果我听事件并获取信息。从扩展中,我可以使用 Selenium 提取它们吗?那是我的问题。很明显,在扩展中我可以获取这些信息并在扩展中使用它们。但我需要使用 Selenium 自动化该过程。
    • 恐怕你不能这样做,因为notifications API 会触发操作系统通知。它们不属于浏览器。
    • 谢谢。但是如果我使用其他东西(不是通知,说打开新标签并在那里写信息而不是通知),我可以搜索标签标题,例如从 Selenium (尽管 Selenium 最初打开了第一个标签,比如:google .com,但扩展打开了一个新标签并写了一些信息。我想得到)。
    猜你喜欢
    • 2011-09-26
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-03
    • 1970-01-01
    • 2017-11-23
    • 2016-01-16
    相关资源
    最近更新 更多