问题
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