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 8211255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:22:09+00:00 2026-06-07T10:22:09+00:00

given the following div element <div class=info> <a href=/s/xyz.html class=title>title</a> <span class=a>123</span> <span class=b>456</span>

  • 0

given the following div element

<div class="info">
    <a href="/s/xyz.html" class="title">title</a>
    <span class="a">123</span>
    <span class="b">456</span>
    <span class="c">789</span>
</div>

I want to retrieve contents of the span with class “b”. However, some divs I want to parse lack the second two spans (of class “b” and “c”). For these divs, I want the contents of the span with class “a”. Is it possible to create a single XPath expression that selects this?

If it is not possible, is it possible to create a selector that retrieves the entire contents of the div? ie retrieves

<a href="/s/xyz.html" class="title">title</a>
<span class="a">123</span>
<span class="b">456</span>
<span class="c">789</span>

If I can do that, I can use a regex to find the data I want. (I can select the text within the div, but I’m not sure how to select the tags also. Just the text yields 123456789.)

  • 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-07T10:22:10+00:00Added an answer on June 7, 2026 at 10:22 am

    The xpath expression should be something like:

    //div/span[@class='b'] | //div[not(./span[@class='b'])]/span[@class='a']
    

    The expression left of the union operator | will select you all the b-class spans inside all divs, the expression on the right hand side will first query all divs that do not have a b-class span and then select their a-class span. The | operator combines the results of the two sets.

    See here for selecting nodes with not() and here for combining results with the | operator.

    Also, to refer to the second part of your question have a look here.
    Using node() in your xpath you can select everything (nodes + text) that is below the node selected. So you can get everything in the div returned by

    //div/node()
    

    for future processing by other means.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an HTML like following: ...more html above... <div class=any_name> <p>Element A goes here</p>
Given the following HTML: <div class=foo>howdy howdy howdy how</div> <div class=bar>Hello</div>​ and the following
Given the following HTML <div classmyclass>10</div> <div classmyclass>25</div> <div classmyclass>50</div> <div classmyclass>20</div> I want
Newbie question. Given the following html. <div id=mycontainer1 class=container> <input type=text class=name/> <input type=text
for the following html <div> <div class=col1 > I dont want to select this
I have the following html code: <div class=Radios> <span class=radio> <input type=radio value=1> yes
Given the following HTML fragment: <div class=word> <input type=text name=A /> <input type=text name=n
Consider the following code: <html> <head></head> <body> <div id='test' class='blah'> <a href='http://somesite.com/' id='someLink'>click!</a> </div>
Given the following html... <div id=group> <ul> <li><div class=category>one</div></li> <li><div class=category>one</div></li> <li><div class=category>onetwo</div></li> <li><div
Given the following HTML page: <html> <head> <title>WatiN Test</title> </head> <body> <div> <p>Hello World!</p>

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.