Hmmm, that's not the full code either but never mind.
In addition, you haven't got a simple 'link and link.nextlink()' condition.
Anyway, is the following really doing what your comments say?
What if you change it to...
In addition, you haven't got a simple 'link and link.nextlink()' condition.
Anyway, is the following really doing what your comments say?
While link And link.nextlink() And prevPathLink Or nextPathLink
What if you change it to...
while link and link.nextlink and (prevpathlink or nextpathlink)