【发布时间】:2022-01-15 15:13:57
【问题描述】:
我正在尝试设置一个 hello world 应用程序来学习 Selenium。我想编写一个打开 Brave 网络浏览器的脚本。
我在网上和堆栈溢出处尝试了一些代码脚本,但似乎没有任何效果。
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();
上面的脚本不识别brave所以我把forBrowser("brave)改成"chrome",还是不行。
我在网上找到了下面的代码,它可以让我得到我想要启动的浏览器的确切目录,但我不知道如何将它与我已有的结合起来。
const chrome = require('selenium-webdriver/chrome');
chrome.setDefaultService(new chrome.ServiceBuilder('/usr/share/applications/google-chrome.desktop').build());
我在另一个thread 阅读了下面的代码,但我不确定如何使用它启动浏览器实例。
const chrome = require('selenium-webdriver/chrome')
const chromeOptions = new chrome.Options()
chromeOptions.setChromeBinaryPath('/usr/bin/brave-browser')
编辑
下面的代码是我最近尝试将网络上的方法拼凑起来以完成这项工作。
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');
我收到一条错误消息“错误:无法在当前 PATH 上找到 ChromeDriver。”
我不确定如何继续
编辑 我启动了 Chrome。这是我所做的:
我已更新到最新的 Chrome 浏览器。 我在此处下载了与我的 Chrome 版本一致的最新 chrome 驱动程序:
https://www.selenium.dev/documentation/webdriver/getting_started/install_drivers/
我在桌面上创建了一个随机目录,例如 blah/code 并将下载的文件放在那里。
然后我打开一个终端窗口并将该目录添加到我的计算机路径变量中,如下所示:
export PATH=$PATH:/some/new/path
我保持当前终端窗口打开并打开 node.js 脚本和 chrome。
我现在需要知道如何更改它以便它打开 Brave
【问题讨论】:
-
你关注这个链接stackoverflow.com/questions/47158434/…了吗?
-
我阅读并更新了我的帖子。以下代码不会引发错误,但我不知道如何使用它来启动浏览器实例: const chrome = require('selenium-webdriver/chrome') const chromeOptions = new chrome.Options() chromeOptions.setChromeBinaryPath(' /usr/bin/brave-browser')
-
我对JS语法了解不多,所以不回答。但我知道,您需要 chrome 驱动程序 exe 和勇敢的文件位置来设置浏览器实例。
-
我都有,只是不知道如何将它们正确地合并到代码中
标签: javascript node.js selenium selenium-webdriver brave