【问题标题】:Selenium: How to scroll in inner div using selenium in pythonSelenium:如何在 python 中使用 selenium 在内部 div 中滚动
【发布时间】:2017-12-29 19:35:54
【问题描述】:

想要滚动 web.whatsapp.com 中的聊天。已分享以下伪代码:

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

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

提前致谢!

【问题讨论】:

    标签: javascript python selenium


    【解决方案1】:

    试试下面的代码

    recentList = driver.find_elements_by_xpath("//div[@class='_2wP_Y']") 
    
    for list in recentList :
        driver.execute_script("arguments[0].scrollIntoView();", list )
        // other operation
    

    【讨论】:

    • 你有解决办法滚动到最后一次聊天吗?
    • 最后一次聊天是recentList 集合中的最后一个元素。无需循环滚动浏览集合中的每个元素,只需滚动最后一个即可。
    • 尝试将list 更改为list[:-1] 进行最后一次聊天。
    【解决方案2】:

    试试这个:

    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)
    

    【讨论】:

    • 请扩展此答案以提供更多上下文
    【解决方案3】:

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

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

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

    【讨论】:

      【解决方案4】:

      仅使用 Python 的解决方案(使用 hTouchActions class' 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()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-03
        • 2014-11-29
        • 1970-01-01
        • 2021-01-19
        • 1970-01-01
        • 1970-01-01
        • 2017-11-26
        • 2019-05-11
        相关资源
        最近更新 更多