1

I am trying to setup a hello world app to learn Selenium. I want to write a script that opens the Brave web browser.

I tried a few code scripts online and at Stack overflow and nothing seems to work.

const { Builder } = require("selenium-webdriver")

async function example() {

    let driver = await new Builder().forBrowser("brave").build();
    console.log(driver)
    await driver.get("https://www.youtube.com/");



}


example();

The above script does not recognize brave so I changed forBrowser("brave) to "chrome". It still doesn't work.

I found the following code online that allows me to get the exact directory of the browser I want to launch, but I do not know how to combine it with what I already have.

const chrome = require('selenium-webdriver/chrome');


chrome.setDefaultService(new chrome.ServiceBuilder('/usr/share/applications/google-chrome.desktop').build());

I read the code below at another thread but I am not sure how to launch a browser instance using it.

const chrome = require('selenium-webdriver/chrome')
const chromeOptions = new chrome.Options()

chromeOptions.setChromeBinaryPath('/usr/bin/brave-browser')

EDIT

The code below is my latest attempt at cobbling together methods from around the web to make this work.

const webdriver = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const chromeOptions = new chrome.Options()
chromeOptions.setChromeBinaryPath('/home/wktdev/Desktop/selenium/drivers/chrome/chromedriver')


let driver = new webdriver.Builder()
    .forBrowser('chrome')
    .setChromeOptions(chromeOptions)
    .build();


driver.get('http://example.com');

I get an error saying "Error: The ChromeDriver could not be found on the current PATH. "

I am unsure how to proceed

EDIT I have Chrome launching. Here is what I did:

I updated to the latest Chrome Browser. I downloaded the newest chrome driver that mirrors my version of Chrome here:

https://www.selenium.dev/documentation/webdriver/getting_started/install_drivers/

I created a random directory on the desktop such as blah/code and I put the downloaded files there.

I then opened a terminal window and added that directory to my computers path variable like this:

export PATH=$PATH:/some/new/path

I kept the current terminal window open and rand the node.js script and chrome opened.

I now need to know how to change this so that it opens Brave instead

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
William
  • 4,422
  • 17
  • 55
  • 108
  • Did you follow this link https://stackoverflow.com/questions/47158434/how-to-run-selenium-tests-on-the-brave-web-browser ? – cruisepandey Dec 11 '21 at 06:21
  • I read it and updated my post. The following code does not throw an error but I have no idea how to use it to launch a browser instance: const chrome = require('selenium-webdriver/chrome') const chromeOptions = new chrome.Options() chromeOptions.setChromeBinaryPath('/usr/bin/brave-browser') – William Dec 11 '21 at 07:02
  • I don't know much about the JS syntax, so I won't answer. But I do know that, you would need chrome driver exe and brave file location to set the browser instance. – cruisepandey Dec 11 '21 at 07:04
  • I have both, I just don't know how to incorporate them into the code properly – William Dec 11 '21 at 08:13

2 Answers2

1

As Brave went full-Chromium (starting from version 0.57), to open browser you need to set setChromeBinaryPath towards the brave executable as follows:

const webdriver = require('selenium-webdriver');
const chromeOptions = new chrome.Options()
chromeOptions.setChromeBinaryPath('/usr/bin/brave-browser')

let driver = new webdriver.Builder()
    .forBrowser('brave')
    .setChromeOptions(chromeOptions)
    .build();

driver.get('http://example.com');

References

You can find a couple of relevant detailed discussions in:

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

You need to specify the path to the brave executable with setChromeBinaryPath. For example (on Windows):

chromeOptions.setChromeBinaryPath('C:/Program Files/BraveSoftware/Brave-Browser/Application/brave.exe')

For me, I copied the chromedriver.exe file to my project folder. But you should also be able to place it anywhere in your PATH environment variable.