这是一种对我有用的不同方法,其中涉及 滚动到 最后一个搜索结果的 视图 ,并等待其他元素加载后再次滚动:
# -*- coding: utf-8 -*-from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.common.exceptions import StaleElementReferenceExceptionfrom selenium.webdriver.support import expected_conditions as ECclass wait_for_more_than_n_elements(object): def __init__(self, locator, count): self.locator = locator self.count = count def __call__(self, driver): try: count = len(EC._find_elements(driver, self.locator)) return count >= self.count except StaleElementReferenceException: return Falsedriver = webdriver.Firefox()dep_airport = ['BTS', 'BRU', 'PAR']arr_airport = 'MAD'dep_date = '2015-07-15'arr_date = '2015-08-15'airports_string = str(r'%20').join(dep_airport)dep_airport = airports_stringurl = "https://www.pelikan.sk/sk/flights/list?dfc=C%s&dtc=C%s&rfc=C%s&rtc=C%s&dd=%s&rd=%s&px=1000&ns=0&prc=&rng=1&rbd=0&ct=0" % (dep_airport, arr_airport, arr_airport, dep_airport, dep_date, arr_date)driver.maximize_window()driver.get(url)wait = WebDriverWait(driver, 60)wait.until(EC.invisibility_of_element_located((By.XPATH, '//img[contains(@src, "loading")]')))wait.until(EC.invisibility_of_element_located((By.XPATH, u'//div[. = "Poprosíme o trpezlivosť, hľadáme pre Vás ešte viac letov"]/preceding-sibling::img')))while True: # TODO: make the endless loop end results = driver.find_elements_by_css_selector("div.flightbox") print "Results count: %d" % len(results) # scroll to the last element driver.execute_script("arguments[0].scrollIntoView();", results[-1]) # wait for more results to load wait.until(wait_for_more_than_n_elements((By.CSS_SELECTOR, 'div.flightbox'), len(results)))笔记:
- 您将需要弄清楚何时停止循环-例如,以某个特定
len(results)
值 wait_for_more_than_n_elements
是自定义的“ 预期条件”,有助于确定何时加载下一部分,我们可以再次滚动



