0

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);
}
gradusas
  • 53
  • 1
  • 7
  • When it fails I run the same selector from console and it shows the DOM object just fine. Thats the most puzzling to me. I will try to copy it on next failure. – gradusas Jan 30 '14 at 13:45
  • this doesn't seem to actually look for buttons, but rather elements with a `button` class. That being said, what happens when it runs into an element of class `button` that has no inner text? – Jeremy Holovacs Jan 30 '14 at 14:13
  • Yes, it is not a real button element. It is a `div` with `button` class then `a` inside it, which I need. As I inspect post-factum on error it is always there. – gradusas Jan 30 '14 at 14:28

2 Answers2

1

At what point does your crawler run that selector? If the page isn't completely finished rendering yet, because the DOM isn't ready, or the code on the page maybe hasn't finished building out the page, your call to $(".button a").last() would return an empty array. Since you can access any index off that empty array still, when you try to grab the first item out of it you'll get undefined, which would then result in an error when you try to call .innerText off of it.

Try logging how many items are being returned by the selector to see if that is the case.

chinabuffet
  • 5,278
  • 9
  • 40
  • 64
  • Crawler script is injected from Chrome extension. It runs on "document_idle" so I assume full DOM is there. – gradusas Jan 30 '14 at 14:32
  • Do you know if the specific page it's breaking on is dynamically building buttons after the DOM has finished loading? Any luck reproducing the error or logging the result of the selector before calling innerText on the result? – chinabuffet Jan 30 '14 at 16:12
  • Good idea. I will try to `console.log` selector's result before going to `if` statement tomorrow. – gradusas Jan 30 '14 at 16:28
  • Ok, now this is driving me crazy. Error happened and `console.log(lastButton)` shows element selected: `1` But I still got error for innerText. This is of course after about a 100 successful parses. – gradusas Jan 31 '14 at 09:13
0

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!

gradusas
  • 53
  • 1
  • 7