【问题标题】:Python Using Adblock with Selenium and Firefox WebdriverPython 使用带有 Selenium 和 Firefox Webdriver 的 Adblock
【发布时间】:2014-01-16 21:40:19
【问题描述】:

我的目标是通过 Python 将 Adblock Plus 与 Selenium 一起使用。我已经能够让它加载扩展,但默认情况下它不包括默认过滤器“EasyList”。这是我目前所拥有的:

 from selenium import webdriver
 from time import sleep
 ffprofile = webdriver.FirefoxProfile()
 adblockfile = '/Users/username/Downloads/adblock_plus-2.4-tb+fx+an+sm.xpi'
 ffprofile.add_extension(adblockfile)
 ffprofile.set_preference("extensions.adblockplus.currentVersion", "2.4")
 browser = webdriver.Firefox(ffprofile)
 while(True):
    browser.get("www.cnn.com")
    sleep(5)

大部分代码都是从http://selenium-python.readthedocs.org/en/latest/faq.html 盗取的

【问题讨论】:

  • 听起来好像超出了 Selenium 的范围,基本上你想更改扩展的设置。我怀疑这是否可以实现。

标签: python selenium firefox-addon adblock


【解决方案1】:

实际上,Adblock Plus 默认会添加 EasyList - 但如果您将 extensions.adblockplus.currentVersion 首选项设置为禁用更新/首次运行操作,则不会。我猜你的目标是阻止首次运行页面出现,但它也阻止了数据存储初始化。请注意,这里还有更多问题:即使 Adblock Plus 添加了 EasyList,下载时间仍然未知。

更好的做法是使用现有的adblockplus/patterns.ini 文件初始化您的配置文件。您可以使用 EasyList 和其他过滤器设置从您的常规 Firefox 配置文件中获取此文件,并将其复制到 /Users/username/Downloads/profilemodel/adblockplus/patterns.ini。那么以下应该可以工作:

ffprofile = webdriver.FirefoxProfile("/Users/username/Downloads/profilemodel");

【讨论】:

    【解决方案2】:

    有更好的方法来做到这一点:

    1) 使用 7-zip 或同等格式提取 adblock.xpi

    2) 使用常规文本编辑器打开 /modules/AppIntegration.jsm

    3) 找到“notifyUser()”的函数声明,并将其替换为简单的返回。例如:

    /**
    * function notifyUser()
    * {
    *   let wrapper = (wrappers.length ? wrappers[0] : null);
    *   if (wrapper && wrapper.addTab)
    *   {
    *       wrapper.addTab("chrome://adblockplus/content/ui/firstRun.xul");
    *   }
    *   else
    *   {
    *       Utils.windowWatcher.openWindow(wrapper ? wrapper.window : null,
    *                                                                    "chrome://adblockplus/content/ui/firstRun.xul",
    *                                                                    "_blank", "chrome,centerscreen,resizable,dialog=no", null);
    *   }
    * }
    */
    
    function notifyUser()
    {
        return;
    }
    

    现在您只需将文件打包回 zip,并将扩展名从 .zip 更改为 .xpi -- 瞧!

    这将阻止 adblock 加载欢迎页面,但仍会配置必要的订阅设置。确保打电话

    ffprofile.set_preference("extensions.adblockplus.currentVersion", "x.x.x")
    

    否则,它不会知道“引导自己”

    请注意,这是针对 adblock_plus-2.0.3 的,因为我使用的是 firefox-17。代码可能略有不同,并且在新版本的不同位置。见:https://issues.adblockplus.org/ticket/206#comment:5

    【讨论】:

      【解决方案3】:

      我最近才通过反复试验发现了如何在没有任何问题的情况下阻止首次运行页面:

      1. 转到您的扩展程序所在的文件夹

      2. lib/adblockplus.js 中,将defaults.suppress_first_run_page 设置为true 而不是false

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-01-11
        • 2021-06-08
        • 2020-12-21
        • 1970-01-01
        • 2018-06-11
        • 1970-01-01
        • 2016-11-28
        相关资源
        最近更新 更多