Skip to content
Advertisement

Selenium retry URL when ValueError

How can I retry url from list if ValueError?

Error:

raise ValueError("No tables found")
ValueError: No tables found

Or other exceptions.

Can use if ValueError then driver.refresh(2wice)

but I dont know location in the code:

if __name__ == '__main__':

    results = None

    for url in urls:
        game_data = parse_data(url)
        if game_data is None:
            continue
        result = pd.DataFrame(game_data.__dict__)
        if results is None:
            results = result
        else:
            results = results.append(result, ignore_index=True)

Maximum retry 2

Advertisement

Answer

You can put the entire thing in a try/except block and if it encounters a ValueError at some point, you can put the same code under except and it will retry it.

if __name__ == '__main__':

    results = None

    for url in urls:
        try:
            game_data = parse_data(url)
            if game_data is None:
                continue
            result = pd.DataFrame(game_data.__dict__)
            if results is None:
                results = result
            else:
                results = results.append(result, ignore_index=True)
        except ValueError:
            game_data = parse_data(url)
            if game_data is None:
                continue
            result = pd.DataFrame(game_data.__dict__)
            if results is None:
                results = result
            else:
                results = results.append(result, ignore_index=True)

Throw in a finally: after the except block with the same spacing if you want it to do something after it’s tried twice.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement