while true
while true

Reputation: 856

Error when trying to getting table cell value

I'm trying to create an array of IP address and port number from this website. but the problem is I get a error

TypeError: table.rows[i].cells[0].getElementsByTagName(...)[0] is undefined

This is my code:

var table = document.getElementsByClassName('DataGrid')[0];
var n = table.rows.length;
console.log(n);
var list="";

for(var i=1;i<n;i++){

    //var ip = table.rows[i].cells[1].innerHTML;
    var port = table.rows[i].cells[0].getElementsByTagName("a")[0].innerHTML; // error here
    //list+="{"+ip+","+port+"}";

}

first cell value pattern is like below.

<td><script type="text/javascript">IPDecode("")</script><a href="http://www.freeproxylists.net/1.179.183.85.html">1.179.183.85</a></td>

so I think I'm doing it right. Am I doing some silly mistake ?

Note: I debug and already found there are some rows which doesn't have a second cells, but problem is I get a error. I don't get at least one output expect length. See the image:

enter image description here

Upvotes: 0

Views: 838

Answers (1)

Domino
Domino

Reputation: 6768

Some rows of this table contain adds and do not follow the row(cell(a)) structure you're trying to get. Whenever you get the result of a get...By... you should check if it is null, and if it is just skip over the loop with continue.

Upvotes: 1

Related Questions