I am finding broken links on webpage by finding all anchor tags.
But there are some dynamically generated href's through javascript.
When I print the list of all links , i get StaleElementReferenceException
due to dynamically generated link.
Why do I get StaleElementReferenceException
for the below twitter link ?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>
<body>
<div style="padding-top:10px;">
<a href="https://twitter.com/url" class="twitter-follow-button" data-show-count="false" data-size="large" data-show-screen-name="false">Follow @url</a>
<script>!function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0], p = /^http:/.test(d.location) ? 'http' : 'https';
if (!d.getElementById(id)) {
js = d.createElement(s);
js.id = id;
js.src = p + '://platform.twitter.com/widgets.js';
fjs.parentNode.insertBefore(js, fjs);
}
}(document, 'script', 'twitter-wjs');</script>
</div>
</body>
Selenium code :
List<WebElement> links=driver.findElements(By.tagName("a")); for(WebElement link: links) { System.out.println(link.getAttribute("href")); }