在TimeoutException之后继续

时间:2016-10-31 03:31:16

标签: python selenium selenium-webdriver

我正在填写一个非常长的列表,并将其中的大块文章粘贴到textform但由于某种原因,网站需要花费一些时间来处理列表,我得到TimeoutException并且我认为我能抓住它然后继续,但我不确定如何从最后quote继续?或者是否有人有任何其他建议如何处理此错误?或者也许一起避免它?

for lst in chunks:
    textform.send_keys([quote+'\n' for quote in lst])
    try:
        pushQuotes = driver.find_element_by_xpath('//input[@type="submit"]')
        pushQuotes.click()
        textform = driver.find_element_by_name('quoteNumbers')
        time.sleep(3)
    except TimeoutException as ex:
        driver.refresh()
        textform = driver.find_element_by_name('quoteNumbers')
        textform.clear()

1 个答案:

答案 0 :(得分:0)

您可以使用enumerate来跟踪您所处的位置:

for i, lst in enumerate(chunks):
    textform.send_keys([quote+'\n' for quote in lst])
    try:
        pushQuotes = driver.find_element_by_xpath('//input[@type="submit"]')
        pushQuotes.click()
        textform = driver.find_element_by_name('quoteNumbers')
        time.sleep(3)
    except TimeoutException as ex:
        # now you can use i to keep track of where you were before the exception
        # here I'm taking a slice of chunks, starting with i
        for j, lst in enumerate(chunks[i:]):
           driver.refresh()
           textform = driver.find_element_by_name('quoteNumbers')
           textform.clear()