【问题标题】:I'm making a bot that likes every post that aren't liked yet我正在制作一个机器人,它喜欢所有尚未被喜欢的帖子
【发布时间】:2019-12-05 17:51:39
【问题描述】:

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

我已经尝试过像标签名称这样的差异化方法


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 结束

【问题讨论】:

  • 请注意,一个好的堆栈溢出问题是关于一个特定问题的,只需要最短的代码来隔离该问题。因此,在一个理想的世界中,在提出问题之前,可以隔离出不工作的特定 XPath 查询,并编写其他人可以运行的最短测试工具,以亲眼看看它不工作。

标签: python selenium


【解决方案1】:

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)

【讨论】:

  • 输入第二个代码后:Message: element click intercepted: Element is not clickable at point (634, 5)。其他元素会收到点击:
    ...
    (Session info: chrome=75.0.3770.142) x 10
  • 打开您的浏览器控制台并检查 xpath 是否找到您想要的。本文应该可以帮助您入门:stackoverflow.com/questions/22571267/…。一旦你有了正确的 xpath,就使用它。
  • 如您在此处看到的:photos.app.goo.gl/vjuwtcGmTJ3uiLtJA 机器人可以看到按钮但无法单击它。还有更多的证据表明,如果错误代码:消息:元素点击被拦截:元素 在点 (69, 20) 不可点击.其他元素会收到点击:
    ...
    (会话信息:chrome=75.0.3770.142)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-27
  • 2020-11-14
  • 2017-03-21
  • 2020-11-23
  • 2019-11-07
相关资源
最近更新 更多