【发布时间】:2020-06-06 06:03:48
【问题描述】:
我正在尝试在 nightwatch.js 中编写一个简单的脚本,该脚本将打开 Google,并在搜索字段中输入文本。我在使用 setValue() 方法向元素发送文本时遇到了问题。
这是我的脚本:
module.exports = {
'Search bar displayed on Google Home Page'(driver) {
driver
.url('http://www.google.com')
.assert.urlContains('google')
.assert.title('Google')
.waitForElementPresent('input[title="Search"]')
.pause(5000)
.setValue('input[title="Search"]', 'test123') // error happens here
.end()
},
}
使用setValue() 时,我看到以下错误:
运行 .setElementValue() 协议操作时出错:TypeError [ERR_UNESCAPED_CHARACTERS]:尝试为“/wd/hub/session/ed0680ce58544facf2a4b193eccbc223/element/[object Object]/value”创建 HTTP 请求时出错:请求路径包含未转义人物 在新的 ClientRequest (_http_client.js:115:13) 在 Object.request (http.js:42:10) 在 HttpRequest.createHttpRequest 在新的承诺 () 在 Selenium2Protocol.sendProtocolAction
出于某种原因,.setValue() 正尝试将 Object object 作为请求 URL 中的 WebElement ID 发送。
脚本成功执行assert和.waitForElementPresent('input[title="Search"]'),所以我知道该元素在页面上。我添加了pause(5000) 以确保页面在尝试发送密钥之前有足够的时间加载。我还尝试在 .keys() 之前运行 .click() 以尝试使元素成为焦点。
我相信语法是正确的,但我还是守夜人的新手,所以这也可能是一个问题。
这个用户和我的问题几乎一模一样,但没有答案:setValue method in Nightwatch is not working
我使用的是chromedriver版本80.0.3987.16
我正在使用nightwatch版本1.3.4
我使用npm install chromedriver 安装了chromedriver 到npm,并在我的nightwatch.json 文件中将路径设置为chromedriver.exe:
{
"src_folders" : ["tests"],
"output_folder" : "reports",
"custom_commands_path" : "",
"custom_assertions_path" : "",
"page_objects_path" : "./page_objects",
"globals_path" : "",
"selenium" : {
"start_process" : true,
"server_path" : "./node_modules/selenium-standalone/.selenium/selenium-server/3.141.5-server.jar",
"log_path" : "./reports",
"host": "127.0.0.1",
"port" : 4444,
"cli_args" : {
"webdriver.chrome.driver" : "./node_modules/chromedriver/lib/chromedriver/chromedriver.exe",
"webdriver.gecko.driver" : "",
"webdriver.edge.driver" : ""
}
},
"test_settings" : {
"default" : {
"launch_url" : "http://localhost",
"selenium_port" : 4444,
"selenium_host" : "localhost",
"silent": true,
"screenshots" : {
"enabled" : false,
"path" : ""
},
"desiredCapabilities": {
"browserName": "chrome",
"marionette": true,
"javascriptEnabled": true,
"acceptSslCerts": true
}
},
"chrome" : {
"desiredCapabilities": {
"browserName": "chrome"
}
}
}
}
有人可以帮我理解这里的问题吗?
【问题讨论】:
-
你是通过npm下载chromedriver还是手动下载?
-
我通过 npm --
npm install -g chromedriver安装了 chromedriver,并在nightwatch.json中为"webdriver.chrome.driver": "C:/Path/To/chromedriver.exe"设置了路径。我已经编辑了我的问题以包含我的完整nightwatch.json文件。 -
我无法在 npm 中找到该版本。我不确定在全球范围内安装它是否有任何影响。我有项目github.com/spnraju/nightwatchjs-selenium-example/blob/master/…。我已将您的测试添加到其中,并且运行良好。我正在检查最新版本的 chromedriver。
-
也适用于最新版本 (80.0.1) 的 chromedriver。尝试卸载当前的 chromedriver 并安装最新版本。
-
感谢您根据自己的回购检查。我将检查我的环境变量并确保我的路径中也没有杂散的
chromedriver.exe。运行chromedriver -v给我输出ChromeDriver 80.0.3987.16 (320f6526c1632ad4f205ebce69b99a062ed78647-refs/branch-heads/3987@{#185})
标签: javascript selenium nightwatch.js