Daniel Daniel - 16 days ago 4
Java Question

JSOUP- Trying to find a specific a href

enter image description here

I am trying to find the circled link as seen in the image above at imgur on a webpage.Currently(as seen below) I am just pulling all a hrefs from the document and looping through it looking for the one that contains "pdf" as it is the only one on the page,is there any way to just pull a href where title = "Download offers in store" or something like that

Document doc = Jsoup.connect("http://www.dunnesstores.com/offer20/food-wine/fcp-category/home").get();
Elements links = doc.select("a[href]" + );

for (Element link : links) {
System.out.println(link.attr("href"));
if (link.attr("href").contains("pdf")){
pdfLink = link.attr("href");
}
}

Answer

You could specify a selector that matches a attribute and its value.

String pdfLink = null;

Document doc = Jsoup.connect("http://www.dunnesstores.com/offer20/food-wine/fcp-category/home").get();
Elements links = doc.select("a[title=\"Download offers in store\"]");

for (Element link : links) {
    pdfLink = link.attr("abs:href");
}

System.out.println(pdfLink);

This selects every a tag where the title attribute is equals Download offers in store.

If you want to search the element by the file ending .pdf you could change the selector to:

a[href$=".pdf\"]