質問

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);
}
役に立ちましたか?

解決 2

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!

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top