PhantomJS的行为与Firefox Webdriver不同

PhantomJS的行为与Firefox Webdriver不同,第1张

PhantomJS的行为与Firefox Webdriver不同

有两个关键问题帮助我解决了这一问题:

  • 不要使用我之前为您提供帮助的自定义等待
  • 将第
    window.document.body.scrollTop
    一个设置为0,然后
    document.body.scrollHeight
    连续设置为

工作代码:

results = []while len(results) < 200:    results = driver.find_elements_by_css_selector("div.flightbox")    print len(results)    # scroll    driver.execute_script("arguments[0].scrollIntoView();", results[0])    driver.execute_script("window.document.body.scrollTop = 0;")    driver.execute_script("window.document.body.scrollTop = document.body.scrollHeight;")    driver.execute_script("arguments[0].scrollIntoView();", results[-1])

版本2 (无限循环,如果滚动条上没有任何内容,则停止):

results = []while True:    try:        wait.until(wait_for_more_than_n_elements((By.CSS_SELECTOR, "div.flightbox"), len(results)))    except TimeoutException:        break    results = self.driver.find_elements_by_css_selector("div.flightbox")    print len(results)    # scroll    for _ in xrange(5):        try: self.driver.execute_script("""     arguments[0].scrollIntoView();     window.document.body.scrollTop = 0;     window.document.body.scrollTop = document.body.scrollHeight;     arguments[1].scrollIntoView(); """, results[0], results[-1])        except StaleElementReferenceException: break  # here it means more results were loadedprint "DONE. Result count: %d" % len(results)

请注意,我已在

wait_for_more_than_n_elements
预期条件下更改了比较。替换为:

return count >= self.count

与:

return count > self.count

版本3 (从页眉滚动到页脚多次):

header = wait.until(EC.visibility_of_element_located((By.TAG_NAME, 'header')))footer = wait.until(EC.visibility_of_element_located((By.TAG_NAME, 'footer')))results = []while True:    try:        wait.until(wait_for_more_than_n_elements((By.CSS_SELECTOR, "div.flightbox"), len(results)))    except TimeoutException:        break    results = self.driver.find_elements_by_css_selector("div.flightbox")    print len(results)    # scroll    for _ in xrange(5):        self.driver.execute_script(""" arguments[0].scrollIntoView(); arguments[1].scrollIntoView();        """, header, footer)        sleep(1)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5668414.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存