javascript to read links from website code example

Example 1: all links in web page

All the link TagName should be "a". 
I locate the webelements starts with tagname "a". 
I also use driver.findElements instead of driver.findElement since the 
list of webelements will return.

List <WebElement>  list = driver.findElements(By.tagName(a))

Example 2: javascript retrieve all page urls of blog posts

var x = document.querySelectorAll("a");var myarray = []for (var i=0; i<x.length; i++){var nametext = x[i].textContent;var cleantext = nametext.replace(/\s+/g, ' ').trim();var cleanlink = x[i].href;myarray.push([cleantext,cleanlink]);};function make_table() {    var table = '<table><thead><th>Name</th><th>Links</th></thead><tbody>';   for (var i=0; i<myarray.length; i++) {            table += '<tr><td>'+ myarray[i][0] + '</td><td>'+myarray[i][1]+'</td></tr>';    };     var w = window.open("");w.document.write(table); }make_table()

Tags:

Misc Example