简体   繁体   中英

XPath Text or Child Node

I have a Div element on a page which is used to display error messages. This div is invisible unless it is populated.

Unfortunately, the content type is inconsistent. In some cases it can have a direct child text node, which I could pick up with:

contains(text(),'*'))

or it can contain a <span> which contains its own child text node, which I could pick up with:

//div/span[contains(text(),'*')]

As much as I would like to, I cannot change this behaviour.

Example 1 :

<div id="TopErrorMessageDiv">Error Message</div>

Example 2 :

<div id="TopErrorMessageDiv">
    <span style="something">Error Message 1<br>Error Message 2</span>
</div>

Fortunately, I really don't need to know the contents, I just need to know if the div is non-empty.

Can anyone help me out?

Try this one

//div[@id="TopErrorMessageDiv" and .//text()]

to match div that contains any text content

这也应该为您工作

//div[@id="TopErrorMessageDiv" and string-length(text()) > 0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM