Mark Zacharski
Mark Zacharski

Reputation: 37

How to select an element with specific text from the HTML a element using JSoup

I got an a element with few attributes one of them is data-product-id this is my element that I want.

for example data-product-id="002212" I am intrested in the number "002212"

My problem is that there can be couple a elements with this link

There is how link looks like.

<a href="something.com" title="test tile" class="title-product" data-jsevent="obj:title--product" data-product-name="test" data-product-id="002212" ddata-product-price="1.99" data-product-brand="test"  data-product-quantity="1"> 

I did something like this:

Elements links = document.select("a.title-product");

I receives every a element with class title-product now How can I get from received html data-product-id but with my number 002212?

I can't parse links to String.

I also tried something like this:

if(links.contains("data-product-id=\"002212\"")){
                        System.out.println("it works");
                    } else {
                        System.out.println("nothing");
                    }

But links.contains equals always "false" even this number is there.

also I tried

it works but I get only first element with for example number 002211 instead of 002212

String linktext = a.attr("data-product-id");

and this is null

String linktext = a.attr("data-product-id=\"002212\"");

Upvotes: 0

Views: 63

Answers (1)

Mark Zacharski
Mark Zacharski

Reputation: 37

Solved this line below did it.

Elements links = document.select("a[data-product-id=\"002212\"]");

Upvotes: 0

Related Questions