首页 > 解决方案 > 在 Selenium 中打开测试页面

问题描述

我开始使用 Selenium,所以我创建(我认为)是一个快速测试脚本,可以简单地在我公司的 Intranet 中打开一个页面。

我不确定我应该在哪里下载 Selenium 的依赖项,所以我在我的开发服务器上创建了一个名为“TESTING”的文件夹,并使用命令提示符安装 Selenium:

 npm install selenium-webdriver

安装似乎是成功的,因为它显示“添加了来自 79 个贡献者的 46 个包......”

在我的 TESTING 目录中,我可以看到添加了 node_modules 目录以及 package-lock.json 文件。

然后我创建了一个 index.php 文件,它只有以下内容:

<h1>hello world</h1>
<button type="submit" name="btnI">Click Me</button>

这里是 sample.js,它包括以下应该打开 index.php 的脚本:

var webdriver = require('selenium-webdriver');

var driver = new webdriver.Builder().
        withCapabilities(webdriver.Capabilities.chrome()).
        build();

driver.get("https://development.usa.company.com/testing/index.php");

driver.findElement(webdriver.By.name('btnI')).click();

我回到 cmd 提示符并运行以下命令:

node sample.js

我收到以下错误:

Error: The ChromeDriver could not be found on the current PATH. 
Please download the latest version of the ChromeDriver from 
http://chromedriver.storage.googleapis.com/index.html 
and ensure it can be found on your PATH.

我究竟做错了什么?

标签: javascriptseleniumtesting

解决方案


看起来您需要将 PATH 添加chromedriver.exe到系统环境变量中。

  1. 安装 chromedriver.exe -- 注意安装目录的文件路径
  2. 在 Windows 中搜索Environment Variables并单击“环境变量..”进行编辑
  3. 编辑“路径”变量
  4. 在出现所有其他文件路径后,添加“C:\Path\To\ChromeDriver\Directory\chromedriver.exe”,用分号与其他路径分隔。

本指南可能会有所帮助:https ://developers.refinitiv.com/sites/default/files/How%20To%20Add%20ChromeDriver%20To%20System%20Variables_0.pdf


推荐阅读