我是 Selenium 新手,我编写了 JS 来运行我的第一个脚本。
我完成了所有设置: npm 初始化 -y npm install --save selenium-webdriver chromedriver geckodriver
我写了以下脚本:
const {By, Key, Builder} = require("selenium-webdriver");
async function test_case() {
let driver = await new Builder().forBrowser("chrome").build();
await driver.get("https://google.com");
await driver.findElement(By.name("q")).sendKeys("Hello, World!", Key.RETURN);
setInterval(function() {
driver.quit();
}, 10000);
}
test_case();
然后我尝试使用
执行脚本节点测试.js
它抛出错误:“错误:无法获取浏览器驱动程序。”
这是我的package.json:
{
"name": "golf",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"chromedriver": "^124.0.1",
"geckodriver": "^4.4.0",
"selenium-webdriver": "^4.20.0"
}
}
所以,我明白发生了什么事。当我加载网络驱动程序时,我的防病毒软件正在删除 .exe: npm install --save selenium-webdriver chromedriver geckodriver
创建了没有 .exe 的驱动程序。
为了解决这个问题,我关闭了防病毒软件并重新运行: npm install --save selenium-webdriver chromedriver geckodriver