Is there a way to choose a div in Jsoup that lacks an id or any attributes?

I am facing a challenge where I have to choose a specific div using Jsoup. Traditionally, divs can be selected based on their id or class attributes using getElementById() for IDs and getElementsByClass() for classes. However, the div that I need to select has a unique structure like this...

<div><h2 class='title'>Example</h2> 
.....
......
...... </div>

My task is to pinpoint this particular div based solely on the text value "Example" within the <h2> tag. This presents a new set of challenges as I cannot use the traditional methods. Any suggestions on how I can achieve this? Your help is much appreciated. Thank you.

Answer №1

Consider using the following CSS selector:

Elements elements = doc.select("div:has(h2)");

This code will target any div that contains an h2 tag. To narrow down your selection even further, you can use the following variation:

Elements elements = doc.select("div:has(h2:contains(Example))");

This will specifically target div elements with an h2 tag that also includes the text "Example" (not case sensitive).

You can explore more ways to combine selector syntax by visiting

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the proper way to mention "a" within an unsorted list?

I currently have a div section with the following content: <ul id = "issues"> <li> <h1>title</h1> <p>text text text </p> <a class="next" href="#">next</a> </li> <li> <h1>title</h1> &l ...

Is there a way to split each foreach value into distinct variables?

I am looking to assign different variables to foreach values. I have fetched data from an API in JSON format, and then echoed those values using a foreach loop. My goal is to display the echoed value in an input box using JavaScript. I attempted the follow ...

Embedding a countdown timer in basic HTML code

Attempting to embed the following into an HTML website: The issue I am facing is that when I run it, the timer does not appear. However, when I run it in fsFiddle, I do see the countdown timer. What could be causing this problem? <!DOCTYPE html> & ...

Passing data from Angular component (.ts) to its corresponding template (.html) file through ngOnInit() method

I have a task involving Angular 18 that requires updating the html page based on the response value of ngOnInit(). During testing, I noticed that the test and maxPage values of ngOnInit() displayed on the html component are test = "ngOnInit" and maxPage = ...

How can I determine if the browser or webdriver has been closed by the user in Selenium?

Operating System: Windows 10 Browser: Chrome WebDriver Browser Version: Chrome 63.0.3239.10 (64-bit) Selenium Version 2.44 Added the following dependency : <dependency> <groupId>org.seleniumhq.selenium</groupId> ...

Maintain consistent width of a page across different mobile devices

Currently, the content on my page does not have any viewport settings. It is simply using a 25% left and right margin for the entire content area. The issue arises when accessing the page from a mobile device, as the width adjusts to match the screen size ...

The "Splash Screen Div" page displayed during transitions and page loading

Creating a "Splash Screen Div" for a loading page involves waiting until everything is loaded and then hiding or moving the div off screen. Below is an example: index.html <div id="loading-Div"> <div id="bear-Logo"> < ...

Pointer Cursor in CSS

I am experiencing a strange issue. When I set the cursor attribute value directly as a string like return ( <Grid item> <Card sx={{ padding: "1rem", ":hover": { cursor: "pointer" ...

Issues with arranging DIV elements using CSS

I have organized a few DIVs within a parent DIV. I am trying to make them align horizontally, but despite my efforts, they remain stacked vertically. Here is the code snippet: <style> #entrycontainer { height: 100px; width: 500px; } #entry ...

Fluid Design - Extra Spacing at the Bottom with Percent Margin-Top

Currently, I am working on developing a website using only percentages. However, I have encountered an issue with my main content section. I have set a margin-top of 10%, but this is causing excessive spacing at the bottom of the page and resulting in a sc ...

Implement a captivating hover effect on every single element within a particular class

I created a basic website with the following design: However, there is an issue with the hover effect. It only works on the specific area where the mouse pointer is located (here ipsum). My intention was for the hover effect to work on all elements. To fi ...

Determine whether a particular fragment is currently being displayed

I am looking for a solution to handle the "onBackPressed" event and check the currently displayed fragment Essentially, I want to create a scenario where pressing the "back" button triggers my app to verify if the fragment_main is being shown. If it is, t ...

Unable to modify the value of an HTML dropdown list

My latest project is a website located at pg.wcyat.me (Source code), where I utilized github.com/thdoan/pretty-dropdowns for dropdown menus. Using JavaScript, I am able to dynamically change the values of select lists. For example: document.getElementById( ...

Utilize a function within styled-components

I am looking to dynamically adjust the font size of a button based on the length of a passed-in prop. However, I can't use a dynamic width due to a custom animation applied to the component. Here is my current code: const checkLength = () => { i ...

Unable to retrieve table header information when clicking on a table cell

My code is working perfectly, except for the fact that when I click on a cell, I cannot retrieve the table header text. Retrieving the row text works fine based on the code. If I use Object.keys(data) inside the alert function to retrieve the data, it give ...

What is the method for determining the width of a Mat-Table once it has been displayed?

When utilizing Angular Material Mat-Table in conjunction with Angular 8, I am passing the dataSource dynamically. The number of rows and columns varies each time. Is there a method to calculate the width of the table once it is rendered on the screen? &l ...

Does anyone know how to designate a Thumbnail when playing Audio on iOS Safari?

I recently launched a website to showcase my new podcast. The audio is embedded in a media player on the page, and when it's playing, it shows up on the Control Center audio tab and even on the lock screen. However, the thumbnail displayed is just a ...

Can anyone tell me the best way to access the name attribute of an HTML element in TypeScript?

Currently, my code is utilizing the name attribute to verify whether the user has entered information in a specific field and validating the input. However, I am facing an issue where the submit button remains active even if there are empty fields presen ...

The Express error is thrown when attempting to read the property 'get' of an undefined value

Currently in the process of organizing my routes in a separate directory: server.js: var express = require("express"), parse = require("body-parser"), db = require("mysql"), mailer = r ...

Why am I not able to see the initial two letters of my first name and the final two letters of my last name?

I attempted to create a mobile application that would display generated text by using the first two letters of your first name and the last two letters of your last name, but unfortunately, it did not function as expected. import java.util.Scanner; p ...