Python Selenium 如果未找到则跳至下一步
我制作了一个简单的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这段代码应该可以工作,它会尝试遵循,如果我不起作用,请转到下一篇文章。
This code should work, It will try to follow and if i doesn't work, go to the next post.