我从未使用过勇敢的浏览器,所以感谢您提出这个问题。
@flydev 指出了勇敢的开关。
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";
}
再次来自 Brave 代码库:
// New users are shown the P3A notice via the welcome page.
registry->RegisterBooleanPref(kP3ANoticeAcknowledged, first_run);
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;
}
我尝试将这些开关与
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())
不幸的是,这不起作用。当我检查 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()
在使用配置文件之前,您必须在 Brave 浏览器中禁用这些项目。
这是带有配置文件 1 且没有确认通知框的浏览器。