为了账号安全,请及时绑定邮箱和手机立即绑定

Selenium 的 cicle 超时异常

Selenium 的 cicle 超时异常

吃鸡游戏 2023-06-27 10:40:11
这是我的代码:for i in matches:    driver.get(i)    Country = WebDriverWait(driver, 2).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[4]/div[5]/div/div/div[1]/section/ul/li[3]/a"))).text    leagueseason = WebDriverWait(driver, 2).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[4]/div[5]/div/div/div[1]/section/header/h1/a"))).text    Date = WebDriverWait(driver, 2).until(EC.visibility_of_element_located((By.ID, 'match-date'))).text    Date = Date.replace(".", "/").replace("-", "").replace(" ", "", 1)    Home = WebDriverWait(driver, 2).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[4]/div[5]/div/div/div[1]/section/ul[2]/li[1]/h2/a"))).text    Away = WebDriverWait(driver, 2).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[4]/div[5]/div/div/div[1]/section/ul[2]/li[3]/h2/a"))).text    ft = WebDriverWait(driver, 2).until(EC.visibility_of_element_located((By.ID, 'js-score'))).text    Res = ""        try:        extrainfo = driver.find_element_by_xpath("//*[@id='js-eventstage']").text    except NoSuchElementException:        extrainfo = " "现在我会TimeoutException在页面未加载时添加一个,我尝试这样做:try:   #previous codeexcept TimeoutException:   driver.get(i)这样,当我遇到超时错误时,代码将跳过 url 抓取并继续处理下一个 url。我该如何解决这个问题?我希望在超时的情况下它会重新加载页面并再次抓取该网址的数据。
查看完整描述

1 回答

?
大话西游666

TA贡献1817条经验 获得超14个赞

您可以将其放入 while 循环中,一旦成功完成,就会继续。所以像这样:


success = False

while success == False:

    try:

        # previous code

        success = True

    except TimeoutException:

        print ('Will try again...')


查看完整回答
反对 回复 2023-06-27
  • 1 回答
  • 0 关注
  • 119 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信