我正试图在nightwatch.js中编写一个简单的脚本,该脚本将打开谷歌,并在搜索字段中输入文本。我在使用两种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_CHARTERS]:尝试为"/wd/hub/session/ed0680ce58544facf2a4b193eccbc223/element/[object object]/value"创建HTTP请求时出错:请求路径包含未标注大小的字符在新的ClientRequest(_http_client.js:111:13(位于Object.request(http.js:42:10(在HttpRequest.createHttpRequest在new Promise((在Selenium2Protocol.sendProtocolAction
由于某种原因,.setValue()
正试图将Object object
作为请求URL中的WebElement
ID发送。
脚本成功地执行了assert
和.waitForElementPresent('input[title="Search"]')
,所以我知道元素在页面上。我添加了一个pause(5000)
,以确保在尝试发送密钥之前页面有足够的时间加载。我还尝试在.keys()
之前运行.click()
,以尝试将元素集中到焦点中。
我相信语法是正确的,但我对守夜还是个新手,所以这也可能是个问题。
这个用户和我有几乎完全相同的问题,但没有答案:Nightwatch中的setValue方法不工作
我使用的是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"
}
}
}
}
有人能帮我理解这里的问题吗?
通过在我的nightwatch.json
文件中的"chrome"
下添加"w3c": false
,问题得到了解决。
特别感谢@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
字段加上"w3c": false
,现在一切都按预期进行。