使用Selenium与NodeJS和Brave浏览器(在MacOS Big Sur上)



我正在尝试在NodeJS应用程序上运行一些简单的测试,使用Selenium和Brave浏览器。

我正在运行Big Sur(11.5.2),使用Brave版本:

Version 1.28.105 Chromium: 92.0.4515.131 (Official Build) (x86_64)

我已经下载了ChromeDriver,我已经安装在我的路径:

> which chromedriver
/Users/dd/bin/scripts/chromedriver
> chromedriver --version
ChromeDriver 92.0.4515.107 (87a818b10553a07434ea9e2b6dccf3cbe7895134-refs/branch-heads/4515@{#1634})

我已经创建了一个文件夹,yarn initited,添加了selenium-webdriver作为开发依赖,


{
"name": "seleniumtest",
"version": "1.0.0",
"description": "seleniumtest",
"main": "index.js",
"license": "MIT",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1"
},
"devDependencies": {
"eslint": "^7.32.0",
"jest": "^27.0.6",
"nodemon": "^2.0.12",
"selenium-webdriver": "^4.0.0-beta.4"
},
"scripts": {
"start": "node ./index.js"
}
}

运行以下Javascript文件index.js:

const { Builder, By, Key, until } = require('selenium-webdriver');
(async function example() {
let driver = await new Builder().forBrowser('brave').build();
try {
await driver.get('http://www.google.com/');
await driver.findElement(By.name('q')).sendKeys('webdriver', Key.RETURN);
await driver.wait(until.titleIs('webdriver - Google Search'), 1000);
} finally {
await driver.quit();
}
})();

我已经启动了chromedriver:

> chromedriver          
Starting ChromeDriver 92.0.4515.107 (87a818b10553a07434ea9e2b6dccf3cbe7895134-refs/branch-heads/4515@{#1634}) on port 9515
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.

但是当我运行yarn start时,我得到:

UnhandledPromiseRejectionWarning: Error: Do not know how to build driver: browser; did you forget to call usingServer(url)?

如果我用Builder().forBrowser('chrome').build();替换Builder().forBrowser('brave').build();,那么Chrome就会启动,但窗口只是坐在那里试图在url栏中加载data:,。我尝试了chromium-与brave相同的结果。

我做错了什么?

您可以使用setChromeBinaryPath函数并将勇敢的二进制路径指向它:

const chrome = require('selenium-webdriver/chrome');    
(async function helloSelenium(){
let options = await new chrome.Options();
options.setChromeBinaryPath('/opt/brave.com/brave/brave-browser');

中的forBrowser('brave')将其替换为'chrome',因为selenium会使用该切换情况,并且只有浏览器enum中的值才有效(chrome, edge, firefox, internet_explorer, safari, opera);

相关内容

  • 没有找到相关文章

最新更新