简体   繁体   中英

Xpath for specific child element of a parent element which is not distinguished by class?

The html snippet is like this:

<div class="busi-attr">

           <p><span class="attrName">Min. Order: </span>1 Piece</p>
           <p><span class="attrName">Supply Ability: </span>10,000&nbsp;&nbsp;Piece/Pieces  per  Month</p>
    </div>

I only want the second <p> element that is amount 10,000 and nothing else, how do i do that? thanks

Try xpath with a list comprehension that checks the text value of an element:

span=[x for x in yourwebelement.xpath('//span[@class="attrName"]') if 'supply ability' in x.text.lower()][0]

This is only the span of course, but all you need now is the parent

p=span.xpath('.//parent::p')[0]

I think the span block stops you from getting the text value of p, so let's get all the text minus whatever is in the span.

text=[x for x in p.itertext() if x != span.text]

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