Python Selenium 如果未找到则跳至下一步

发布于 2025-01-15 07:38:37 字数 944 浏览 4 评论 0原文

我制作了一个简单的 IG 机器人,它会转到指定的标签、逐个帖子、点赞并关注海报。但有时 Instagram 会崩溃并且不显示帖子 - 然后脚本也会崩溃,因为它找不到关注/喜欢按钮。

如果找不到按钮,我可以做一个循环或其他什么,单击下一个帖子按钮吗?我对 python 完全陌生,我无法弄清楚。

这是我的代码:

# Following the poster
        if followtag:
            self.Wait(1,2)
            temp = self.driver.find_element_by_xpath('/html/body/div[6]/div[3]/div/article/div/div[2]/div/div/div[1]/div/header/div[2]/div[1]/div[2]/button')
            if (temp.text == "Obserwuj") and (name not in self.doNotFollowList):
                temp.click()
                self.AddFollowedList(name)
                self.AddDoNotFollowList(name)
                print("[{}] *followed* {}".format(i+1, name))

# Go to the next post
        if i == 0:
            self.driver.find_element_by_xpath('/html/body/div[6]/div[2]/div/div/button').click()
        else:
            self.driver.find_element_by_xpath('/html/body/div[6]/div[2]/div/div[2]/button').click()

I've made a simple IG bot that goes to indicated tag, goes post by post, likes, and follow the poster. But sometimes Instagram crashes and doesn't show the post - then script also crashes because it can't find a follow/like button.

Can I make a loop or something else if the button is not found, click the next post button? I'm totally new to python and I cant figure it out.

Here's my code:

# Following the poster
        if followtag:
            self.Wait(1,2)
            temp = self.driver.find_element_by_xpath('/html/body/div[6]/div[3]/div/article/div/div[2]/div/div/div[1]/div/header/div[2]/div[1]/div[2]/button')
            if (temp.text == "Obserwuj") and (name not in self.doNotFollowList):
                temp.click()
                self.AddFollowedList(name)
                self.AddDoNotFollowList(name)
                print("[{}] *followed* {}".format(i+1, name))

# Go to the next post
        if i == 0:
            self.driver.find_element_by_xpath('/html/body/div[6]/div[2]/div/div/button').click()
        else:
            self.driver.find_element_by_xpath('/html/body/div[6]/div[2]/div/div[2]/button').click()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难以启齿的温柔 2025-01-22 07:38:37

这段代码应该可以工作,它会尝试遵循,如果我不起作用,请转到下一篇文章。


    try:
        # Following the poster
        if followtag:
            self.Wait(1,2)
            temp = self.driver.find_element_by_xpath('/html/body/div[6]/div[3]/div/article/div/div[2]/div/div/div[1]/div/header/div[2]/div[1]/div[2]/button')
            if (temp.text == "Obserwuj") and (name not in self.doNotFollowList):
                temp.click()
                self.AddFollowedList(name)
                self.AddDoNotFollowList(name)
                print("[{}] *followed* {}".format(i+1, name))

    except:
        # Go to the next post
        if i == 0:
            self.driver.find_element_by_xpath('/html/body/div[6]/div[2]/div/div/button').click()
        else:
            self.driver.find_element_by_xpath('/html/body/div[6]/div[2]/div/div[2]/button').click()

This code should work, It will try to follow and if i doesn't work, go to the next post.


    try:
        # Following the poster
        if followtag:
            self.Wait(1,2)
            temp = self.driver.find_element_by_xpath('/html/body/div[6]/div[3]/div/article/div/div[2]/div/div/div[1]/div/header/div[2]/div[1]/div[2]/button')
            if (temp.text == "Obserwuj") and (name not in self.doNotFollowList):
                temp.click()
                self.AddFollowedList(name)
                self.AddDoNotFollowList(name)
                print("[{}] *followed* {}".format(i+1, name))

    except:
        # Go to the next post
        if i == 0:
            self.driver.find_element_by_xpath('/html/body/div[6]/div[2]/div/div/button').click()
        else:
            self.driver.find_element_by_xpath('/html/body/div[6]/div[2]/div/div[2]/button').click()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文