1

I'm able to lauch the Brave Browser using Selenium, ChromeDriver and Python

Code trials:

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/")

But I'm unable to get rid of the product analytics notification bar almost similar to the Google Chrome notification bar.

barve_product_analytics_notification_bar

Can anyone help me out?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

3 Answers3

7

I have never used the Brave Browser, so thanks for opening this question.

@flydev had pointing out the Brave switches.

Switches in pref_names.cc in Brave's code base

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

namespace brave {

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

} 

Again from the Brave code base:

// 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;
}

I tried to use these switches with selenium.webdriver.common.desired_capabilities.DesiredCapabilities.

Code snippet

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())

Unfortunately this did not work. When I checked Brave Browser's github account I found this item:

As you can see this is a known issue. I will keep looking for a method that uses DesiredCapabilities.

In the meantime, you can pass a Brave profile to the driver, which suppresses the acknowledgement notification.

Here is the code:

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()

You have to disable these items in the Brave Browser before using the profile.

enter image description here

Here is the browser with Profile 1 and no acknowledgement notification box.

enter image description here

Life is complex
  • 15,374
  • 5
  • 29
  • 58
  • I was going to link this unanswered github issue explaining why I written the answer here, I added a sentence about the `capabilities` – flydev Jan 03 '22 at 08:47
1

I think it's not possible anymore and I don't really know the real reason behind this, but there is at least two reasons.

The first is that the command-line switch --p3a-upload-enabled was optional and only available before Brave beta-test.
And the second, it's certainly related to privacy, GDPR and internal policies as the P3A Brave feature doesn't collect personal information but still collect telemetry data with software quality in mind.

You will get more information about this part on their blog: https://brave.com/privacy-preserving-product-analytics-p3a/


As argument, you can take a look at this commit, more precisely on lines L13, L79-L80 and L212-L221 where they removed the switch --p3a-upload-enabled.


To get back to your issue, if you are not requried to run chromedriver profileless, then just set a profile, once the browser is running, you can close the notification bar, the closed state will be saved in the profile and on the next run, the bar will be hidden.

The following code will create a profile when the WebDriver is instanciated:

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

options = Options()

# set profile path
options.add_argument("user-data-dir=C:\\BrowserDrivers\\Test_Profile\\")
# optional, will be relative to `user-data-dir` switch value
options.add_argument("--profile-directory=test_data")

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/")

And,

Once the WebDriver run, you will find the following config part in the Preferences file which can be found inside the Profile folder.

{
  "brave": {
    "p3a": {
      "enabled": "false",
      "notice_acknowledged": "true"
    }
  },
  "p3a": {
    "last_rotation_timestamp": "13285608876785125"
  }
}

The solution could be to set theses preferences on WebDriver instanciation:

# try to disable P3A via prefs
# ref: https://github.com/brave/brave-core/blob/master/components/p3a/pref_names.cc
chrome_prefs = {"brave":{"p3a":{"enabled":"false","notice_acknowledged":"true"},"widevine_opted_in":"false"},"p3a":{"last_rotation_timestamp":"13285608876785125"}}
options.experimental_options["prefs"] = chrome_prefs

Unfornutely, I couldn't managed to get it working, despite no error - if you do it, please ping me :)

And if you ask me if we can set the capabilities after the instanciation of the WebDriver, then again I would say nop, as of the current implementation of Selenium, once the WebDriver instance is configured with our intended configuration through DesiredCapabilities class and initialize the WebDriver session to open a Browser, we cannot change the capabilities runtime.

flydev
  • 4,327
  • 2
  • 31
  • 36
0

When I search Google for: C# Selenium "Brave uses completely private product analytics to estimate the overall usage of certain features"

This page came up first in the results the rest of the results appearing unrelated.

So, I will post my answer for C# here even though this was a Python question originally. It may help Python coders as well. I upvoted @Life is complex for leading me down the magic path. Thanks

***Note: Because this software updates so often I am also adding the exact versions and download locations that it is working with.

Brave Browser Version: 1.57.47 Chromium: 116.0.5845.96 (Official Build) (64-bit)

Official Chrome Driver Downloads: https://googlechromelabs.github.io/chrome-for-testing

ChromeOptions chromeOptions = new ChromeOptions();

// ...

// Removes: "Brave is being controlled by automated test software."
chromeOptions.AddExcludedArgument("enable-automation");

// Removes: "Brave uses completely private product analytics to estimate the overall usage of certain features."
chromeOptions.AddLocalStatePreference("brave.p3a.notice_acknowledged", true);

return new ChromeDriver(chromeOptions);

Hope that helps.

Happy coding!!!