Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9216163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:21:30+00:00 2026-06-18T02:21:30+00:00

See this html <div> <p> <span class=abc>Monitor</span> <b>$300</b> </p> <a href=/add>Add to cart</a> </div>

  • 0

See this html

<div>
    <p>
    <span class="abc">Monitor</span> <b>$300</b>
    </p>
    <a href="/add">Add to cart</a>
</div>
<div>
    <p>
    <span class="abc">Keyboard</span> $20 
    </p>
    <a href="/add">Add to cart</a>
</div>

Using xpath I want to parse Monitor $300 and Keyboard $20. I use this xpath

 //div[a[contains(., "Add to cart")]]/p/text()

But it selects <span class="abc">Monitor</span> <b>$300</b>. I don’t want the tags. How do I get only the text?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:21:31+00:00Added an answer on June 18, 2026 at 2:21 am

    You want to select all descendant text, not just child text:

    //div[a[contains(., "Add to cart")]]/p//text()
    

    Note the double slash between p and text() there.

    This potentially will also include a lot of inter-tag whitespace though, you you’ll need to clean that up. Example using lxml:

    >>> import lxml.etree as ET
    >>> tree = ET.fromstring('''<div>
    ... <div>
    ...     <p>
    ...     <span class="abc">Monitor</span> <b>$300</b>
    ...     </p>
    ...     <a href="/add">Add to cart</a>
    ... </div>
    ... <div>
    ...     <p>
    ...     <span class="abc">Keyboard</span> $20 
    ...     </p>
    ...     <a href="/add">Add to cart</a>
    ... </div>
    ... </div>''')
    >>> tree.xpath('//div[a[contains(., "Add to cart")]]/p//text()')
    ['\n    ', 'Monitor', ' ', '$300', '\n    ', '\n    ', 'Keyboard', ' $20 \n    ']
    >>> res = _
    >>> [txt for txt in (txt.strip() for txt in res) if txt]
    ['Monitor', '$300', 'Keyboard', '$20']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a HTML code like this: <div class=links nopreview><span><a class=csiAction href=/WebAccess/home.html#URL=centric://REFLECTION/INSTANCE/_CS_Data/null>Home</a></span>&nbsp;•&nbsp;<span><span><a class=csiAction href=/WebAccess/home.html#URL=centric://SITEADMIN/_CS_Site>Setup</a></span>&nbsp;•&nbsp;</span><span><a
I'm using the Google Maps API. Please see this JSON response . The HTML
<div onclick=alert(this.innerHTML);> <p>Since you saw <span title=:P class=emoticon tongue></span> if <strong>you read</strong> the intro
By default Html.ValidationSummary() produces HTML like this: <span class=validation-summary-errors>There were some errors...</span> <ul class=validation-summary-errors>
Please see this example: if (strlen($_SESSION['userDetails']['THISNAME']) == 0) { $_SESSION['userDetails'][''.$THISNAME.'Error'] = autofocus; include $docRoot/html/forms/reg/user_info.html.php;
You can see an interesting table at this link. http://norvig.com/21-days.html#answers The table described, Mutex
Hi i have this HTML code: <tr class=odd events style=> <tr> <a title=Expand to
<div class=row> <label for=txtTitle>Title</label> <div class=holder> <div class=text-holder> <span class=ico-error></span><span class=text> <input id=txtTitle runat=server
I'm iterating over radio buttons previous neighbors. My html looks like this : <div
I have this JSON layout: [ { tag: div, attr: [ [id, sortSideStyleIlluminated], [class,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.