from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
# list of college names
names = []
for name in names:
# search for the college here
# get list of search results
WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, "//div[@class='rc']")))
search_results = driver.find_elements_by_xpath("//div[@class='rc']")
# get first result
search_result = search_results[0]
# attempt to load the page
try:
search_result.click()
except TimeoutException:
# click operation should time out if next page does not load
# pass to move on to next URL
pass
您可以在页面上调用
WebDriverWait
,如果页面捕捉到一个TimeoutException
,那么您将知道加载时间太长,因此可以继续下一个由于您不知道每个页面的HTML会是什么样子,这是一个非常具有挑战性的问题
这是一个非常粗略的概述。正如我所提到的,如果不知道预期的页面标题是什么,或者预期的页面内容是什么样子,那么编写一个能够成功实现这一点的通用方法是非常困难的。这段代码只是你的一个起点
相关问题 更多 >
编程相关推荐