首页 > 解决方案 > 我正在制作一个机器人,它喜欢所有尚未被喜欢的帖子

问题描述

问题是它不喜欢这些帖子。

我尝试过标签名称等差异方法


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time


    def like_photo(self):
        driver = self.driver
        driver.get("https://www.instagram.com")
        time.sleep(1)
        for i in range(1, 4):
            driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            time.sleep(2)

        # find all the heart links
        hrefs = driver.find_elements_by_xpath("//span[@aria-label='Synes godt om']")
        pic_hrefs = [elem.get_attribute('href') for elem in hrefs]
        pic_hrefs = [href for href in pic_hrefs]
        print(' Photos ' + str(len(pic_hrefs)))

        for _ in pic_hrefs:
            driver.get("https://www.instagram.com")
            driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            try:
                like_button = lambda: driver.find_elements_by_xpath("//span[@aria-label='Synes godt om']")
                like_button.click()
                time.sleep(18)
            except Exception as e:
                time.sleep(1)


nameIG = InstagramBot(username, password)
nameIG.login()
nameIG.like_photo()


它像任何帖子一样,输出只是:照片4

进程以退出代码 0 结束

标签: pythonselenium

解决方案


exit code 0意味着您的代码正在运行且没有错误。但是,仍然存在问题。

要查看代码中是否存在实际错误,请更改异常操作。

    except Exception as e:
        print(e)  # shows actual error

尝试这个:

like_buttons = driver.find_elements_by_xpath(some_xpath_to_buttons)  # list of WebElements
for button in like_buttons:
    button.click()
    time.sleep(18)

推荐阅读