如果找不到Python Selenium,请跳到下一步



我制作了一个简单的IG机器人,它可以进入指定的标签,逐篇发布,点赞并关注海报。但有时Instagram会崩溃,不显示帖子——然后脚本也会崩溃,因为它找不到关注/点赞按钮。

如果找不到按钮,我可以做一个循环或其他什么吗?点击下一个帖子按钮?我对蟒蛇完全陌生,我想不通。

这是我的代码:

# 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()

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


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()

最新更新