Selenium:如何在python中使用selenium在内部div中滚动

时间:2017-12-29 19:35:54

标签: javascript python selenium

想要在web.whatsapp.com中滚动聊天记录。共享下面的伪代码:

recentList = driver.find_elements_by_xpath("//div[@class='_2wP_Y']")
driver.execute_script("window.scrollTo(0, 500);")

期待一个解决方案,以便在web.whatsapp.com上直到最后一次聊天。

提前致谢!

4 个答案:

答案 0 :(得分:3)

尝试以下代码

recentList = driver.find_elements_by_xpath("//div[@class='_2wP_Y']") 

for list in recentList :
    driver.execute_script("arguments[0].scrollIntoView();", list )
    // other operation

答案 1 :(得分:0)

仅使用 Python 的解决方案(使用 hTouchActions 类'scroll_from_element method):

from selenium.webdriver.common.touch_actions import TouchActions

recent_list = driver.find_elements_by_xpath("//div[@class='_2wP_Y']")
touch_action = TouchActions(driver)
touch_action.scroll_from_element(recent_list[1], 0, 150).perform()

答案 2 :(得分:0)

下面是一个简单的方法,当其他答案都没有时,它对我有用。

首先确保您选择的是带有“滚动条”元素的 div。

scrolling_element= find_element_by_xpath(scrolling_element_xpath)    
driver.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', scrolling_element)

答案 3 :(得分:-1)

尝试一下:

eula = driver.find_elements_by_xpath("//div[@class='_2wP_Y']") 

for my_xpath in eula:
    driver.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', my_xpath)
    time.sleep(1)