I have been trying to identify broken links on a webpage by extracting all anchor tags. However, some of the links are dynamically generated through JavaScript. When I attempt to print out the list of all the links, I encounter a StaleElementReferenceException
error due to these dynamically created links. Can anyone explain why this error occurs specifically for the twitter link provided below?
<!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")); }