javascript Nightwatch 无法通过 css id 或 class 选择器定位元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30059337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Nightwatch can't locate element via css id or class selectors
提问by Ken Smith
We're using Nightwatch to automate some of our UI testing. Some of the current tests are rather brittle, mostly having to do with weird CSS selectors, and I'm trying to simplify them. But some of the simple CSS selectors that I would expect to work are, well, not.
我们正在使用 Nightwatch 来自动化我们的一些 UI 测试。当前的一些测试相当脆弱,主要与奇怪的 CSS 选择器有关,我正在尝试简化它们。但是我希望使用的一些简单的 CSS 选择器实际上不是。
I'm trying to find this deeply nested <a>
tag:
我试图找到这个深度嵌套的<a>
标签:
<a class="btn btn-quote btn-crm" href="/crm/" id="btnEndSession" style="display: inline-block;">End Session</a>
Here's a bit of code that's working:
这是一些有效的代码:
.waitForElementVisible('#quoteSummary > div > div > div > div > a:nth-child(2)', 1000)
.click('#quoteSummary > div > div > div > div > a:nth-child(2)')
But that's a nasty CSS selector, and I'd like to replace it with this:
但这是一个讨厌的 CSS 选择器,我想用这个替换它:
.waitForElementVisible('#btnEndSession', 1000)
.click('#btnEndSession')
As the <a>
element I'm interested in does in fact have the id of btnEndSession
, it seems like that should work. But it doesn't: nightwatch errors out with one of its "Timed out while waiting for element <#btnEndSession> to be visible for 10000 milliseconds" messages.
由于<a>
我感兴趣的元素实际上具有 id btnEndSession
,因此看起来应该可行。但它没有:nightwatch 错误之一是“等待元素 <#btnEndSession> 可见 10000 毫秒时超时”消息。
I've also tried:
我也试过:
.waitForElementVisible('.btn-crm', 10000)
.waitForElementVisible('a[id=btnEndSession]', 10000)
.waitForElementVisible('a#btnEndSession', 10000)
But those don't work either. Any idea why these simpler selectors aren't picking the element up?
但那些也行不通。知道为什么这些更简单的选择器没有选择元素吗?
EDIT:
编辑:
OK, weirdly enough, this worked:
好吧,奇怪的是,这有效:
.waitForElementVisible('a.btn-crm', 1000)
However, in the same test, looking for this element:
但是,在同一个测试中,寻找这个元素:
<select class="custom-form-control crm-result-visit select-hidden" id="crm-result-visit" name="crmResultVisit">
<option value=" ">Select</option>
<option value="Not Home">Not Home</option>
</select>
All of these different selectors fail to find the element:
所有这些不同的选择器都无法找到元素:
.waitForElementVisible('select.crm-result-visit', 10000)
.waitForElementVisible('select#crm-result-visit', 10000)
.waitForElementVisible('#crm-result-visit', 10000)
.waitForElementVisible('.crm-result-visit', 10000)
.waitForElementVisible('select[id=crm-result-visit]', 10000)
I'm sort of reluctantly driven to the conclusion that the CSS selector support in nightwatch is pretty buggy.
我有点不情愿地得出这样的结论,即 nightwatch 中的 CSS 选择器支持非常有缺陷。
FWIW, we're using version 0.6.7 of nightwatch.
FWIW,我们使用的是 0.6.7 版的 nightwatch。
采纳答案by Ken Smith
For what it's worth, we ended up abandoning Nightwatch, and going straight to Selenium (C#, in our case), which didn't seem to have these problems, and integrated better into the rest of our environment. Not a great answer, but I don't have this problem anymore :-).
无论如何,我们最终放弃了 Nightwatch,直接使用 Selenium(在我们的例子中是 C#),它似乎没有这些问题,并且更好地集成到我们的其他环境中。不是一个很好的答案,但我不再有这个问题:-)。
回答by Juhi Saxena
I have just started using NightWatch JS , I am using nightwatch v0.6.7 . Following code is working fine for me
我刚刚开始使用 NightWatch JS ,我正在使用 nightwatch v0.6.7 。以下代码对我来说工作正常
module.exports = {
'1. test' : function (browser) {
browser
.url('<your url>')
.waitForElementVisible('select[id=crm-result-visit]',1000)
.click('select[id=crm-result-visit]');
}
};
回答by Sabin Bhandari
Not sure if helps, but I stumble upon this issue today and got it working after awhile.
不确定是否有帮助,但我今天偶然发现了这个问题,并在一段时间后得到了解决。
By default nightwatch is using the css selector so just adding ".useXpath()" fixed my issue. So, check which selector you are using first and then use the required one.
默认情况下,nightwatch 使用 css 选择器,因此只需添加“.useXpath()”即可解决我的问题。因此,请先检查您使用的是哪个选择器,然后使用所需的选择器。
来源:https: //github.com/nightwatchjs/nightwatch-docs/blob/master/guide/using-nightwatch/using-xpath-selectors.md