简体   繁体   English

如何使用 nodejs 的 selenium web-driver 获取子元素的数量

[英]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-wrapjs-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 .现在要获取计数,您可以获得xyzlength属性。 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;
}

EDIT编辑

When you call the function:当你调用函数时:

getCount().then(function(count) {
  // your stuff there
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Selenium Web驱动程序获取元素的绝对位置 - Get the absolute position of an element with Selenium web-driver 无法使用Selenium Web驱动程序python从自动完成下拉列表中选择多个选项 - Unable to select multiple options from autocomplete drop-down using selenium web-driver python Selenium与JavaScript和Nodejs:如何计算页面上的元素? - Selenium with JavaScript and Nodejs: How to count elements on page? 如何使用 Python 驱动程序在 Selenium 和 web 驱动程序中获取部分文本 - How to get a part of text in Selenium and web driver using Python 如何使用硒驱动程序单击元素? - How to click on elements using selenium driver? 如何使用 selenium 和 nodejs 获取整个页面源代码? driver.page_source 返回 undefined - How to get entire page source using selenium and nodejs? driver.page_source is returning undefined Selenium 与 JS 如何计算父元素中的子元素 - Selenium with JS how to count child elements inside parent 如何使用Selenium Web驱动程序在代码镜像中输入值? - How to input value in code mirror using selenium web driver? 如何使用Selenium Web驱动程序单击导航栏 - How to click on navigation bar using selenium web driver 如何使用Node.js驱动程序在MongoDB中获取当前操作 - How to get current operations in mongodb using the nodejs driver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM