首页 > 解决方案 > NightwatchJS 无法从元素列表中找到第二个元素

问题描述

我正在尝试查找产品图片列表,然后找到第二张产品图片以单击它,但 nightwatch 找不到它。该页面是https://www.artsyjewels.com/collections/earrings
有人可以告诉我如何点击这个吗?谢谢。

xpathproduct: (.//div[@class="yit-wcan-container"]//following-sibling::div[@class="jas-product-image pr oh jas-product-image-equal"])[2]

NightwatchJS can't find the element
.waitForElementVisible('xpath', '@xpathproduct', 1000)

或者,我可以使用 CSS 选择器 :nth-of-type(2) 并且它可以工作,但想知道如何让它与上面的 xpath 一起工作

div.jas-grid-item.jas-col-md-3.jas-col-sm-4.jas-col-xs-6.mt__30.product.has-post-thumbnail.user_custom:nth-of-type(2)>div>div.jas-product-image.pr.oh.jas-product-image-equal>a

标签: nightwatch.js

解决方案


根据 Nightwatch.js 文档的元素属性部分,您可以在页面对象上定义索引以获取与选择器匹配的元素的第二个实例,或者执行类似的操作

.waitForElementVisible({ selector: '@xpathproduct', index: 1 }, 1000)

在调用时更改索引。

注意:我认为文档中关于传递获取第二个元素的索引是不正确的;以我的经验,index: 0获得第一个元素,index: 1您想要获得第二个元素也是如此。


推荐阅读