I am writing a crawler which goes through pages if it detects a ">" type button on current page. Code fails about 1 time out of 100 with error "Uncaught TypeError: Cannot read property 'innerText' of undefined". In attempt to troubleshoot I open Chrome's developer console and test the selector on that specific page manually. It always returns what I expect and the button is visually there. I am puzzled why it fails only rarely and at the same time page where it fails looks normal. Any suggestions how to isolate the problem?
Code:
lastButton = $(".button a").last()[0];
if(lastButton.innerText === ">") {
next(lastButton.href, "getFriends", id);
}
Found that code in question actually works fine 100% of time. The problem is somehow related to my crawler executing a different scrape function without loading the page for that function. So the peculiar intermittent problem is somewhere else. Thank you for all who tried to help!