[英]how to get count of child elements using selenium web-driver for nodejs
I have searched at numerous places but I am not getting an answer我搜索了很多地方,但我没有得到答案
here is my html :这是我的html:
<form id="search_form_homepage" >
...
<div class="search__autocomplete" style="display: block;">
<div class="acp-wrap js-acp-wrap">
<div class="acp" data-index="0"><span class="t-normal">elephant</span>cheap auto</div>
<div class="acp" data-index="1"><span class="t-normal">elephant</span>asia</div>
...
...
<div class="acp" data-index="2"><span class="t-normal">elephant</span>africa</div>
</div>
...
</div>
</form>
I simply need to get the count of the <div>
present within the div
with class acp-wrap js-acp-wrap
我只需要使用类acp-wrap js-acp-wrap
来获取 div 中存在的<div>
div
计数
I can reach this point but am stuck beyond :我可以达到这一点,但被卡住了:
let xyz = driver.findElements(By.className(".acp-wrap js-acp-wrap>div"));
You would need to use By.css
to get element by this: .acp-wrap js-acp-wrap > div
.您需要使用By.css
通过以下方式获取元素: .acp-wrap js-acp-wrap > div
。 Also, your selector is not correct.此外,您的选择器不正确。 When you select an element by class, you need to put a period before the class name: .acp-wrap.js-acp-wrap > div
(remove the space between acp-wrap
and js-acp-wrap
, too).当你按类选择一个元素时,你需要在类名前加上一个句点: .acp-wrap.js-acp-wrap > div
(也去掉acp-wrap
和js-acp-wrap
之间的空格)。
Here is how you can get that element now:这是您现在如何获取该元素的方法:
let xyz = driver.findElements(By.css(".acp-wrap.js-acp-wrap > div"));
Now to get the count, you can get the length
property of xyz
.现在要获取计数,您可以获得xyz
的length
属性。 But since driver.findElement
returns a promise, you need to use async-await.但是由于driver.findElement
返回一个 promise,你需要使用 async-await。 You can create a function:您可以创建一个函数:
async function getCount() {
let xyz = await driver.findElements(By.css(".acp-wrap.js-acp-wrap > div"));
const count = xyz.length;
return count;
}
When you call the function:当你调用函数时:
getCount().then(function(count) {
// your stuff there
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.