user1862963
user1862963

Reputation: 79

looping in python and checking for logic in try/except method

I am not an expert in python and I am not understanding something that seems very basic to me. Here it goes:

I am using selenium to get a webpage, look for a link in a specific position (in fact the link to the "next page") and if the link is present click on it and start all over. My piece of code is this:

check = True
while check:

    #do something

    #check if there is a link to a "next page":

    try:
        nextPageLink=driver.find_element_by_xpath("//div[@class='pgLinks']/a[3]")

        nextPageLink.click()
        time.sleep(timeToCharge)
    except:
        check = False

This code runs only once. It finds the link to the next page, it clicks on it but it not run the loop again, like if it goes through the code in except and puts the variable check to False.

Any idea why this is so and how I should be doing it?

thanks

Upvotes: 0

Views: 153

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599610

The problem is probably that you're catching all exceptions. Something is probably raising an exception in one of the other lines, and your catch-all clause is being triggered.

Don't do that. Find out which exceptions you're expecting, and only catch them. Anything else is a legitimate error and should bubble up.

Upvotes: 3

Related Questions