如何使用 Python 抑制通过 Selenium 和 ChromeDriver 启动的 Brave 浏览器中的产品分析通知栏

Deb*_*anB 1 python selenium google-chrome selenium-chromedriver brave-browser

我可以使用 Selenium、ChromeDriver 和 Python 启动 Brave 浏览器

代码试验:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service

options = Options()
options.binary_location = r'C:\Program Files\BraveSoftware\Brave-Browser\Application\brave.exe'
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get("https://www.google.com/")
Run Code Online (Sandbox Code Playgroud)

但我无法摆脱几乎与Google Chrome通知栏类似的产品分析通知栏。

barve_product_analytics_notification_bar

谁能帮我吗?

Lif*_*lex 5

我从未使用过 Brave 浏览器,所以感谢您提出这个问题。

@flydev 指出了 Brave 开关。

Brave 代码库中 pref_names.cc 中的开关

#include "brave/components/p3a/pref_names.h"

namespace brave {

const char kP3AEnabled[] = "brave.p3a.enabled";
const char kP3ANoticeAcknowledged[] = "brave.p3a.notice_acknowledged";

} 
Run Code Online (Sandbox Code Playgroud)

再次来自 Brave 代码库:

// New users are shown the P3A notice via the welcome page.
registry->RegisterBooleanPref(kP3ANoticeAcknowledged, first_run);
Run Code Online (Sandbox Code Playgroud)
void BraveConfirmP3AInfoBarDelegate::Create(
    infobars::ContentInfoBarManager* infobar_manager,
    PrefService* local_state) {
  
   // Don't show infobar if:
   // - P3A is disabled
   // - notice has already been acknowledged
   if (local_state) {
    if (!local_state->GetBoolean(brave::kP3AEnabled) ||
        local_state->GetBoolean(brave::kP3ANoticeAcknowledged)) {
      local_state->SetBoolean(brave::kP3ANoticeAcknowledged, true);
      return;
    }
  }

  infobar_manager->AddInfoBar(
      CreateConfirmInfoBar(std::unique_ptr<ConfirmInfoBarDelegate>(
          new BraveConfirmP3AInfoBarDelegate(local_state))));
}


void BraveConfirmP3AInfoBarDelegate::InfoBarDismissed() {
  // Mark notice as acknowledged when infobar is dismissed
  if (local_state_) {
    local_state_->SetBoolean(brave::kP3ANoticeAcknowledged, true);
  }
}


bool BraveConfirmP3AInfoBarDelegate::Cancel() {
  // OK button is "Disable"
  // Clicking should disable P3A
  if (local_state_) {
    local_state_->SetBoolean(brave::kP3AEnabled, false);
  }
  return true;
}
Run Code Online (Sandbox Code Playgroud)

我尝试将这些开关与 selenium.webdriver.common.desired_capabilities.DesiredCapabilities.

代码片段

from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

chrome_options = Options()
capabilities = DesiredCapabilities().CHROME

prefs = {
    'brave.p3a.enabled': True,
    'brave.p3a.notice_acknowledged': False
}

chrome_options.add_experimental_option('prefs', prefs)
capabilities.update(chrome_options.to_capabilities())

Run Code Online (Sandbox Code Playgroud)

不幸的是这没有用。当我检查 Brave Browser 的 github 帐户时,我发现了这个项目:

如您所见,这是一个已知问题。我将继续寻找一种使用DesiredCapabilities.

同时,您可以将 Brave 配置文件传递给驱动程序,从而抑制确认通知。

这是代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.binary_location = '/Applications/Brave Browser.app/Contents/MacOS/Brave Browser'
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-popup-blocking")
chrome_options.add_argument("--disable-notifications")

chrome_options.add_argument("user-data-dir=/Users/username/Library/Application Support/BraveSoftware/Brave-Browser/Default")

chrome_options.add_argument("profile-directory=Profile 1")

# disable the banner "Chrome is being controlled by automated test software"
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])

driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver', options=chrome_options)

driver.get('https://www.google.com')

sleep(60)
driver.close()
driver.quit()

Run Code Online (Sandbox Code Playgroud)

在使用配置文件之前,您必须在 Brave 浏览器中禁用这些项目。

在此输入图像描述

这是具有配置文件 1 且没有确认通知框的浏览器。

在此输入图像描述