2

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

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

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 inited,添加了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('brave').build();Builder().forBrowser('chrome').build();然后 Chrome 会启动,但窗口只是坐在那里试图data:,在 url 栏中加载。我试过chromium- 结果与 for 相同brave

我究竟做错了什么?

4

1 回答 1

0

您可以使用 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 将使用它进行切换,并且只有 Browser 枚举中的值才有效(chrome、edge、firefox、internet_explorer、safari、opera);

于 2022-03-01T15:42:12.267 回答