简体   繁体   中英

XPath testing text in child elements?

I have HTML with a table-like format (not <table> ). I need to check if the given row has all the values.

...
<div id="rows">
  <div class="row">
    <p>Java</p>
    <p>Ruby</p>
  </div>
  <div class="row">
    <p>HTML</p>
    <p>Ruby</p>
  </div>
</div>
...

How do I check using xpath if this has Java AND Ruby within a single row ?

I tried //p[text()='Java' and text()='Ruby'] but didn't quite work. Also tried //div[@id='rows']//p[contains(text(),'Java') and contains(text(),'Ruby')]

Try using below xpath :-

//div[normalize-space(.) = 'Java Ruby']

or

//div[@class = 'row' and contains(.,'Java') and contains(.,'Ruby')]

or If you want div with id rows with multiple contains try as :-

//div[@id= 'rows' and contains(.,'Java') and contains(.,'Ruby')]

This XPath,

//div[@class='row' and p='Java' and p='Ruby']

will select all div/@class='row' elements with Java and Ruby string values among the p children.

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