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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:21:01+00:00 2026-06-07T17:21:01+00:00

I am trying to always find the first <ul> that comes after <h2> ,

  • 0

I am trying to always find the first <ul> that comes after <h2>, preferrably without looping, using a single Jsoup select() statement.

For example, that <ul> can be a sibling of <h2> as shown in this HTML snippet:

<!-- lots of things can come before -->

<h2 class="C" id="S3">
<button class="B">Click Me</button>
<span id="targetSpan">Target Span</span>
</h2>

<ul>
<li> List item 1</li>
<li> List item 2</li>
</ul>

<!-- lots of things can come after -->

Or it can be a descendant (not necessarily a direct child!) of a sibling of <h2>. The sibling may or may not be the first sibling element after <h2>, but <ul> is always the first <ul> after that <h2>. For example:

<!-- lots of things can come before -->

<h2 class="C" id="S3">
<button class="B">Click Me</button>
<span id="targetSpan">Target Span</span>
</h2>

<div>
<ul>
<li> List item 1</li>
<li> List item 2</li>
</ul>
</div>

<!-- lots of things can come after -->

I can find the <h2> easily:

Element h2 = select("h2 > span#targetSpan").first().parent();

But once I have h2, how do I find the first <ul> after it? (could be sibling or descendant, I don’t control that HTML code)

  • 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-07T17:21:04+00:00Added an answer on June 7, 2026 at 5:21 pm

    You can’t avoid your own loop. You must iterate through all next element siblings until you find the next <ul>:

      Element h2next = h2.nextElementSibling();
      do {
        ul = h2next.select("ul:not([class]))").first();         
      } while (h2next!=null && ul==null);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find the first property of a class that is an Integer
I find myself always trying to fit everything into the OOP methodology, when I'm
am trying send an array to php file using $_POST ,still always getting an
I am trying to get a layout that always takes up the entire screen,
I'm trying to create a SQL statement to find the matching record based on
I'm trying to find a way that my program will show up a setup-view,
Ok, so I'm trying to create a program using a while loop to find
I always get this error when trying to compile my file with Boost::GIL PNG
I am trying to have a content block always be shown to the user,
I'm trying to test some mailers with rspec but deliveries are always empty. Here

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.