繁体   English   中英

Selenium Webdriver在网站上找不到所有元素

[英]Selenium Webdriver doesnt find ALL elements on website

我一直在开发Instagram自动化程序,但是有一个问题:当我尝试查找所有LIKE按钮并打印它们时

它只喜欢前4张图片,但还有更多,我不知道为什么找不到它们

使用Selenium,Python,Selenium Webdriver,时间,键(来自selenium)代码:

hearts = browser.find_elements_by_xpath("//*[contains(@class, '_eszkz
_l9yih')]")

for i, heart in enumerate(hearts):
    print('{}/{}'.format(i, len(hearts)))
    heart.click()
    time.sleep(2)

这是Instagram加载页面项目的唯一方式

Instagram一次只加载4张图片

需要向下滚动页面以加载更多内容

hearts = driver.find_elements_by_xpath("//*[contains(@class, '_eszkz _l9yih')]")

while True: #infinite loop
    q = len(hearts)
    last_item = hearts[q-1]
    for i, heart in enumerate(hearts):
        print('{}/{}'.format(i, len(hearts)))
    #heart.click()
    sleep(2)
    last_item.location_once_scrolled_into_view
    hearts = driver.find_elements_by_xpath("//*[contains(@class, '_eszkz _l9yih')]")

获取最后一个项目列表,然后滚动到它并重新加载列表

这是完整的代码

暂无
暂无

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

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