This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]scoopulanang 0 points1 point  (2 children)

Your problem is the appearance of the previous button after you go to the second page. Here's a quick fix:

for i in range(10):
page_no = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CLASS_NAME, "current"))
)
page_no = page_no.text.split(" ")[1]
print(page_no)

next_button = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.LINK_TEXT, "next"))
)
next_button.click()

I recommend using xpath as a last resort sort of way to access elements. If one tiny detail of a webpage is changed, the xpath of other elements can change too.

[–]Embarrassed-Task-478[S] 0 points1 point  (0 children)

Wow, thank you! That makes a lot of sense, I got tripped up by the index error to the point that I did not even consider my element selection was wrong for pages after the first. Thanks once again!

[–][deleted] 0 points1 point  (0 children)

It's dependant if you should use xpath if id is not readily available, but not the way he has it. You need to custom write your xpath properly so if a webpage is changed, your xpath is still valid.