selenium.common.exceptions.TimeoutException: Message: timeout超时终止运行解决方法

具体方法就是捕获错误并重新运行程序,案例如下:

# 获取一个地区二手房信息的总页数    
def get_total_page(browser, url):    
	browser.get(url)    
	try:    
		time.sleep(4)    
		total_room = browser.find_element_by_xpath('/html/body/div[4]/div[1]/div[2]/h2/span').text    
		if not total_room:    
			return None    
		if int(total_room) <= 30:    
			return 1    
		total = WebDriverWait(browser, 30).until(    
				EC.presence_of_element_located((By.XPATH, "/html/body/div[4]/div[1]/div[7]/div[2]/div/a[last()]"))    
			)    
		if not total.text.isdigit():    
			total_page = browser.find_element_by_xpath('/html/body/div[4]/div[1]/div[7]/div[2]/div/a[last()-1]').text    
		else:    
			total_page = total.text    
		return total_page    
	except TimeoutException as e:    
		print('获取总页数失败,25秒后重新获取')    
		time.sleep(25)    
		return get_total_page(url)


支付宝扫码打赏 微信扫码打赏

如果本文对你有帮助,欢迎打赏本站

喜欢 ()or分享
    匿名评论
  • 评论
人参与,条评论