Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top