簡體   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