javascript - 不能在 nightwatch.js 中使用 setValue()
问题描述
我正在尝试在 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) at Object.request (http.js:42:10) at HttpRequest.createHttpRequest at new Promise () at Selenium2Protocol.sendProtocolAction
出于某种原因,.setValue()
正在尝试作为请求 URL 中Object object
的ID 发送。WebElement
脚本成功执行assert
and .waitForElementPresent('input[title="Search"]')
,所以我知道该元素在页面上。我添加了一个pause(5000)
以确保在尝试发送密钥之前页面有足够的时间加载。我之前也尝试过跑步.click()
以.keys()
尝试使元素成为焦点。
我相信语法是正确的,但我仍然是守夜人的新手,所以这也可能是一个问题。
这个用户和我有几乎完全相同的问题,但没有答案:Nightwatch 中的 setValue 方法不起作用
我正在使用chromedriver
版本80.0.3987.16
我正在使用nightwatch
版本1.3.4
我chromedriver
通过npm
使用安装并在我的文件中npm install chromedriver
设置路径:chromedriver.exe
nightwatch.json
{
"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"
}
}
}
}
有人可以帮我理解这里的问题吗?
解决方案
通过在我的文件中添加"w3c": false
under解决了这个问题。"chrome"
nightwatch.json
特别感谢@Raju,nightwatch.conf.js
他们的示例存储库中的文件有一个这样的例子。
我改变了这个:
"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
}
}
对此:
"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,
"chromeOptions": {
"w3c": false
}
}
}
添加chromeOptions
字段 plus"w3c": false
现在一切都按预期工作。
推荐阅读
- c# - 当两者都有约束时,从另一个泛型类派生泛型类的语法是什么?
- javascript - 在 React js 表单中预填充图像字段
- firefox - 为什么 Firefox 忽略清单?
- react-native - React Native Elements Checkbox SetState 不是函数
- sql-server - 在 MS sql server-2008 中插入 100 万行
- google-cloud-platform - Ubuntu Compute Engine VPS 上的 Google Cloud IKEv2 VPN
- javascript - 如何在对话流中使用异步函数?
- javascript - required 属性不适用于按钮
- javascript - 如何在画布中的边界矩形底部放置一个按钮
- windows - 为什么 for 循环中的单个 % 在命令行中没有被解释为环境变量,而是在批处理文件中?