栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Selenium缓慢向下滚动页面

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Selenium缓慢向下滚动页面

这是一种对我有用的不同方法,其中涉及 滚动到 最后一个搜索结果的 视图 ,并等待其他元素加载后再次滚动:

# -*- 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
    是自定义的“ 预期条件”,有助于确定何时加载下一部分,我们可以再次滚动


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/436054.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号