Nightwatch.js中的Xpath [英] Xpath in Nightwatch.js

查看:77
本文介绍了Nightwatch.js中的Xpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个结构

<div class ="wg-block">
  ...
    <h4 class ="number" >
       "Text"

我必须确保带有文本"text"的元素h4在div中. 我尝试这个:

I have to make sure that the element h4 with text "text" is in div. I try this:

 .useXpath()
 .waitForElementVisible('/div[contains(@class, "wg-block")]/h4[text()="Text"]',1000)

,但有一个错误. 如何正确确定该元素的可见性?

but have an error. How can I correctly be sure in visibility of this element?

推荐答案

尝试替换

'/div[contains(@class, "wg-block")]/h4[text()="Text"]'

使用

'//div[@class = "wg-block"]//h4[normalize-space(text())="Text"]'

请注意,以/开头的内容适用于root元素(是html,但不是div),并且/也表示直接子级.因此,/div/h4表示h4,它是root元素div的直接子代.

Note that starting / applicable for root element (which is html, but not div) and also / means direct child. So /div/h4 means h4 which is the direct child of a root element div.

您应该使用//div//h4匹配h4,它是div的后代,位于DOM

You should use //div//h4 to match h4 which is descendant of div that is located somewhere in DOM

text()="Text"可以应用于匹配元素<h4>Text</h4>, 但是如果您想匹配

text()="Text" could be applied to match element <h4>Text</h4>, but if you want to match

<h4>
    Text
</h4>

您需要除去空格和换行符.在这种情况下,您可以使用normalize-space(text())方法或contains(text(), "Text")

you need to get rid of spaces and new line characters. In this case you can use normalize-space(text()) method or contains(text(), "Text")

这篇关于Nightwatch.js中的Xpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆