0

When I use this code, I get timeout exception after a while.

driver = webdriver.Firefox()
driver.implicitly_wait(100)


def csv_url_reader(url_obj):
    reader = csv.DictReader(url_obj, delimiter=',')
    for line in reader:
        url = line["URL"]
        driver = webdriver.Firefox()
        driver.get(url)
        try:
            title = WebDriverWait(driver, 100).until(
                EC.presence_of_element_located((By.CLASS_NAME, "some class name with title"))
            ).text
        finally:
            driver.close()
            driver.quit()
        print("Title is " + title)


if __name__ == "__main__":
    with open("url.csv") as url_obj:
        csv_url_reader(url_obj)

CSV File contains about 3 thousand links and after processing two hundredths of them it outputs an error. How can I get around this error? Can I restart the script from the last processed link?

8
  • You didn't specify where it times out exactly? If it's somewhere in code that you can catch, just use try except block and then ignore this timeout (or just log it somewhere) and continue. I'm assuming it's driver.get(url) that times out. Include it in your try except block. Commented May 18, 2019 at 11:23
  • File "find.py", line 318, in <module> EC.presence_of_all_elements_located((By.CLASS_NAME, "class")) File "/home/me :) /PycharmProjects/project/venv/lib/python3.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: Commented May 18, 2019 at 12:19
  • moving the driver to try didn't help. Commented May 18, 2019 at 12:19
  • I think the browser session is over and I'm getting an error Commented May 18, 2019 at 12:34
  • Error appears after 10 or 15 minutes* Commented May 18, 2019 at 12:46

1 Answer 1

1

By the trace you provided in comment, this line is throwing an exception:

title = WebDriverWait(driver, 100).until(
                EC.presence_of_element_located((By.CLASS_NAME, "some class name with title"))
            ).text

You have it in your try block but the try block does not have a corresponding except so... basically the error is not caught. Add an except block and it will work.

Sign up to request clarification or add additional context in comments.

1 Comment

I added except TimeoutException: pass and it's gone, thank you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.