简体   繁体   中英

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

it only likes the first 4 pictures but there are many more and i dont know why it cant find them

Using Selenium, Python, Selenium Webdriver, time, Keys(from selenium) Code:

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 only load 4 pictures at time

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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