简体   繁体   English

使用 python selenium 向下滚动以实现新元素的动态出现

[英]Scrolldown with python selenium to achieve dynamic appearing of new elements

Url is https://donstroy.com/full-search . Url 是https://donstroy.com/full-search On that page, there is parent <div class="content"> and 21 <div class="item"> inside it.在那个页面上,里面有父<div class="content">和 21 个<div class="item"> When human hovers mouse over <div class="content"> and scrolls down, multiple new <div class="item"> elements dynamically appear.当人类将鼠标悬停在<div class="content">并向下滚动时,会动态出现多个新的<div class="item">元素。

I'm trying to acheive this result (appearing of new elements) by selenium but all i can get is only first 21 <div class="item"> .我试图通过 selenium 来实现这个结果(出现新元素),但我只能得到前 21 个<div class="item">

I tried different methods to scrolldown in selenium but none of them resulted in appearing new <div class="item"> elements.我尝试了不同的方法在 selenium 中向下滚动,但它们都没有导致出现新的<div class="item">元素。 Below is my code.下面是我的代码。 The first method with scrollIntoView is working fine when i run it in pure javascript in Element Inspector's console (it causes appearing of new elements) but still not working via selenium.当我在 Element Inspector 的控制台中以纯 javascript 运行它时,使用 scrollIntoView 的第一种方法运行良好(它会导致出现新元素),但仍然无法通过 selenium 运行。

import selenium.webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.action_chains import ActionChains

def parse_objs():
    return driver.find_elements_by_xpath('//div[@class="item"]')

options = Options()
options.headless = True
driver = selenium.webdriver.Firefox(options=options)
driver.set_page_load_timeout(300) # yes, it may take up to multiple minutes to load the page so may need to wait long time, it's normal
driver.get('https://donstroy.com/full-search')

objs = parse_objs()

# Amount of object on initial page load
print('Initial load : %s' % len(objs)) # prints 21 as expected

# Method 1
driver.execute_script('arguments[0].scrollIntoView();', objs[-1])
objs = parse_objs()
print('Method 1 : %s' % len(objs)) # expected to increase but still prints 21
#The thing is that when running the same method in pure javascript in Element Inspector's console, it's working fine:
#var objs=document.getElementsByClassName('item');
#objs[20].scrollIntoView();
#after that, amount of objs (objs.length) increases to 41 so it should work in selenium but it does not!

# Method 2
actions = ActionChains(driver)
actions.move_to_element(objs[-1]).perform()
objs = parse_objs()
print('Method 2 : %s' % len(objs)) # expected to increase but still prints 21

# Method 3
objs_container=driver.find_element_by_xpath('//div[@class="content"]')
driver.execute_script('arguments[0].scrollTop = 300;', objs_container)
print('ScrollTop=%s' % driver.execute_script('return arguments[0].scrollTop', objs_container)) # always returns 0 no matter what value i try to apply for scrollTop in above command
objs = parse_objs()
print('Method 3 : %s' % len(objs)) # expected to increase but still prints 21

driver.quit()

Looking at the website https://donstroy.com/full-search the next set of elements load only after reaching the end of scroll.查看网站https://donstroy.com/full-search下一组元素仅在滚动结束后加载。 Now with all your method it seems like webdriver is reading only the first 21 elements which has been loaded initially along with the page.现在,使用您的所有方法,webdriver 似乎只读取最初与页面一起加载的前 21 个元素。 You may want to see if the scroll is working so that the next set of elements appear dynamically.您可能想查看滚动是否正常工作,以便下一组元素动态显示。 Add an expected conditions wait (EC), print the data and then scroll down again until the next set of data appear.添加预期条件等待(EC),打印数据然后再次向下滚动,直到出现下一组数据。 Repeat this step till you get the desired length of list重复此步骤,直到获得所需的列表长度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM