Xpath in Nightwatch.js

依然范特西╮ 提交于 2019-12-11 01:37:51

问题


I have this structure

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

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?


回答1:


Try to replace

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

with

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

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.

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

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

<h4>
    Text
</h4>

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")



来源:https://stackoverflow.com/questions/42697261/xpath-in-nightwatch-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!