简体   繁体   English

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

[英]Selenium Webdriver doesnt find ALL elements on website

I ve been working on an Instagram automation program but there's one problem: When i try to find all LIKE buttons and print them 我一直在开发Instagram自动化程序,但是有一个问题:当我尝试查找所有LIKE按钮并打印它们时

it only likes the first 4 pictures but there are many more and i dont know why it cant find them 它只喜欢前4张图片,但还有更多,我不知道为什么找不到它们

Using Selenium, Python, Selenium Webdriver, time, Keys(from selenium) Code: 使用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)

Its only due the way instagram loads the pages items 这是Instagram加载页面项目的唯一方式

Instagram only load 4 pictures at time Instagram一次只加载4张图片

Will need to scroll down the page to load more 需要向下滚动页面以加载更多内容

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')]")

Get the last item list, and scroll to it and reload the list 获取最后一个项目列表,然后滚动到它并重新加载列表

Here the full code 这是完整的代码

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

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