How to get href from a parent class?

jsoup get href
how to get href value of anchor tag in javascript
jsoup get attribute value
jsoup select
extract value from html
how to get href value of anchor tag in jquery
how to set href value of anchor tag in javascript
jsoup documentation

I am trying to get all the href links from a parent class which is like this:

<div class="parentClass">
   <article class="className">
      <a href="link1">
   <article class="className">
      <a href="link2">

What is the correct way to locate the href element?

Use below xpath


Use code as below:

  List<WebElement> list=driver.findElements(By.xpath("//div[@class='parentClass']//*[@href]"));

   for(WebElement e : list){
       String link = e.getAttribute("href");
       System.out.println(e.getTagName() + "=" + link);

HTML DOM Anchor href Property, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  href: url: Specifies the linked document, resource, or location. name: Adam Wood. Adam is a technical writer who specializes in developer documentation and tutorials.

If you use below xpath then you will locate all href elements from the parent


Use selector-syntax to find elements: jsoup Java HTML parser, You want to find or manipulate elements using a CSS or jquery-like selector syntax. by class name, e.g. .masthead; [attribute] : elements with attribute, e.g. [​href] class "body"; parent > child : child elements that descend directly from parent,  Get the book free! Often you’ll want your JavaScript functions to access parent elements in the DOM. To accomplish this in JavaScript, try element.parentNode .

The easiest way to do this is to use a CSS selector, div.parentClass a. I would suggest that you also add a wait because the page may take a few seconds to load, depending on how much data it contains. After that, you just need to locate all matching elements, loop through each one and get the href attribute, and print them (or whatever else you want to do with them). The code with these suggestions is below.

List<WebElement> links = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div.parentClass a")));
for (WebElement webElement : links)

Links in HTML documents, Path information: the BASE element. Resolving relative URIs <P>You'll find a lot more in <A href="chapter2.html">chapter two</A>. See also this <A href=". The parent() method returns the direct parent element of the selected element. The DOM tree: This method only traverse a single level up the DOM tree. To traverse all the way up to the document's root element (to return grandparents or other ancestors), use the parents() or the parentsUntil() method.

jsoup - Working with URLs, Jsoup − main class to connect to a url and get the html content. link.attr("href") − provides the value of href present in anchor tag. It may be link.attr("abs:href") − provides the absolute url after resolving against the document's base URI. link. Is this doable? The current behavior will ignore any placed inside the parent tag and just expand the list. It would be great for each list item to have the capability to have a link AND an expand button.

Add Active Navigation Class Based on URL, Ideally you output this class from the server side, but if you can't… Let's say you have navigation like this: <nav> <ul> <li><a href="/">Home</a></li> <li><a  Drop your files here. Drop files here, paste or browse browse

Highlight All Links To Current Page, This function will add the class “selected” to any links (even relative) that point to By using this.href , it's getting the property of the anchor tag's href and not that  Get away from the school, stay in quiet neighborhoods, and definitely stay away from places where your parents or their friends might see you. Walk directly away from the school when you leave the school grounds. Don't pass by any windows or chain link fences where a teacher can see you on the other side. 9

  • See: How do I do X? The expectation on SO is that the user asking a question not only does research to answer their own question but also shares that research, code attempts, and results. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! See also: How to Ask
  • Might want to do a [contains(@class, 'parentClass')] in case the element has one more class name.