Grabbing the Href Attribute of an a Element

Grabbing the href attribute of an A element

Reliable Regex for HTML are difficult. Here is how to do it with DOM:

$dom = new DOMDocument;
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('a') as $node) {
echo $dom->saveHtml($node), PHP_EOL;
}

The above would find and output the "outerHTML" of all A elements in the $html string.

To get all the text values of the node, you do

echo $node->nodeValue; 

To check if the href attribute exists you can do

echo $node->hasAttribute( 'href' );

To get the href attribute you'd do

echo $node->getAttribute( 'href' );

To change the href attribute you'd do

$node->setAttribute('href', 'something else');

To remove the href attribute you'd do

$node->removeAttribute('href'); 

You can also query for the href attribute directly with XPath

$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//a/@href');
foreach($nodes as $href) {
echo $href->nodeValue; // echo current attribute value
$href->nodeValue = 'new value'; // set new attribute value
$href->parentNode->removeAttribute('href'); // remove attribute
}

Also see:

  • Best methods to parse HTML
  • DOMDocument in php

On a sidenote: I am sure this is a duplicate and you can find the answer somewhere in here

JavaScript - get href attribute value via DOM traversal

Edit

I think I should point out (since this has been accepted), what @LGSon had put in the comments and @hev1 has put in their answer, that this should be handled in a more elegant way than simply calling getElement on multiple DOM elements.

Here is a proposed (and much nicer) version of the call to get the href value:

document.querySelector('#something td:nth-child(3) a').href;

Original Answer

The href property exists on the anchor tag (<a>) which is a child of the <td> element. You will need to grab the anchor by using something like the DOM children property like so:

tdElement = document.getElementById('something').getElementsByTagName('td')[3];
anchor = tdElement.children[0];
anchor.getAttribute("href");

Get local href value from anchor (a) tag

The below code gets the full path, where the anchor points:

document.getElementById("aaa").href; // http://example.com/sec/IF00.html

while the one below gets the value of the href attribute:

document.getElementById("aaa").getAttribute("href"); // sec/IF00.html

How to extract the href attribute value from within the closest list item

Two issues:

1) you have closing anchor tag </a> without opening anchor tag as next sibling of div in li. you need to remove it.

2) div elements #tinypic+n are siblings of anchor element. You need to use:

$("#tinypic" + i).siblings("a").attr("href");

or

$("#tinypic" + i).prevAll("a").attr("href");

or

$("#tinypic" + i).closest("li").find("a").attr("href");

Cypress get href attribute

The code below should do what you're trying to achieve. There is also an entire recipe with suggestions on how to test links that open in new tabs.

it('Advertise link should refer to Contact page', () => {
cy.get('div.footer-nav > ul > li:nth-child(2) > a')
.should('have.attr', 'href').and('include', 'contact')
.then((href) => {
cy.visit(href)
})
})

I would also suggest reading through the Cypress document on the best ways to assign and work with variables: https://on.cypress.io/variables-and-aliases

Grab the href and text of a tags from the page

You can do something like following

var arr = [];
$.each( $("a.my-link"), function( i, element ) { // iterate over all anchor elements with class my-link
arr.push({
"LinkID" : i+1,
"LinkHref" : $(element).attr("href"),
"LinkText" : $(element).html()
});
});


Related Topics



Leave a reply



Submit