CountingRows in Web table

I have a routine that recovers a table from a website but for some reason it recovers not quite half of the data. The website is finance yahoo and I want recover the last years closing prices for various stocks for analysis. With Safari open to the correct page running the following scrip returns 102 lines the last of which is the line at the bottom of the table. There are about 254 lines. Using Javascript in AppleScript this is the code

tell application "Safari"
	set TD to do JavaScript ¬
		"var secondTable = document.querySelectorAll('table')[0];
			if (secondTable) {var rows = secondTable.getElementsByTagName('tr');
			var tableData = [];for (var i = 0; i < rows.length; i++) {var rowData = [];
			var cells = rows[i].getElementsByTagName('td');for (var j = 0; j < cells.length; j++) {
			rowData.push(cells[j].innerText);}tableData.push(rowData);}tableData;}" in document 1
end tell

For what it’s worth I think I managed to fix this issue by going to the end of the web page before the Javascript command. Seems strange maybe some can tell me why. This is the sub.

on EndWebpage()
	tell application "System Events"
		tell application "Safari" to activate
		delay 0.5
		key code 119
		delay 0.5
		key code 119
	end tell
	delay 2
end EndWebpage