【发布时间】:2020-12-22 16:12:39
【问题描述】:
好吧,我一直在尝试制作一个 instagram 机器人,它可以发回帖子的 src 链接,但我遇到了问题,我一直收到错误“selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: /html/body/div[4]/div[2]/div/article/div[2]/div/div/div[1]/img" 当我试图用里面的src。谁能帮忙? 链接:https://www.instagram.com/direct/inbox/.
def dm_check(self, driver):
print("dm_check starts here")
try:
self.driver.find_element_by_xpath('/html/body/div[1]/section/div/div[2]/div/div/div[1]/div[2]/div/div/div/div/div[1]/a/div').click()
except:
self.driver.get('https://www.instagram.com/direct/inbox/')
print("Refreshing...")
time.sleep(10)
self.dm_check(self.driver)
print("dm_check stops here")
self.download(self.driver)
def download(self, driver):
print("download starts here")
self.driver.find_element_by_xpath('/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[1]/div/div/div[2]/div[2]/div/div/div/div/div/div/div[2]/img').click()
post = self.driver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[2]/div/div/div[1]/img').get_attribute("src")
print(str(post))
print("download ends here")
self.dmessage(self.driver)
【问题讨论】:
-
好的两件事使用 webdriver 等待并检查它是否在 iframe 下。
-
如果这两个问题不是问题,那就是你的 xpath 问题,所以只需检查元素并从开发人员工具中复制它的 xpath。
-
对不起,我是 selenium 的新手,所以我如何检查它是否是 iframe,这就是我获得 xpath 的方式,我进入检查并复制了它@arundeepchohan
-
去开发者工具搜索iframe,看看它是否是那个标签的父div。
-
如果您的标签有效,它也应该在开发者工具中弹出。