微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用Selenium获取从Python上的Facebook联系人收到的最新消息?

如何解决如何使用Selenium获取从Python上的Facebook联系人收到的最新消息?

我正在尝试使用selenium在python中为Facebook创建一个聊天机器人。到目前为止,我已经拥有了:

    def sendMessageFacebook(self,persons):

    time.sleep(10) #time to close notifications because i dont kNow how to close them
    for person in persons:
        sendMessageBtn = self.driver.find_element_by_xpath('//*[@id="mount_0_0"]/div/div[1]/div[1]/div[2]/div[4]/div[1]/div[3]/span/div/div[1]')
        sendMessageBtn.click()
        time.sleep(3)
        message_to = self.driver.find_element_by_xpath('//*[@id="mount_0_0"]/div/div[1]/div[1]/div[2]/div[4]/div[2]/div/div/div[1]/div[1]/div/div/div/div/div/div/div[1]/div/div[1]/div[1]/div[2]/div[1]/div/div/div/label/input')
        message_to.send_keys(person)
        time.sleep(10)
        person_click = self.driver.find_element_by_xpath('/html/body/div[1]/div/div[1]/div[1]/div[2]/div[4]/div[2]/div/div/div[1]/div[2]/div/div/div[1]/div[1]/div/div/div[1]/ul/div[1]/li/div/div[1]/div')
        person_click.click()
        time.sleep(5)
        person_send = self.driver.find_element_by_css_selector('#mount_0_0 > div > div:nth-child(1) > div.rq0escxv.l9j0dhe7.du4w35lb > div.poy2od1o.i09qtzwb.n7fi1qx3 > div.l9j0dhe7.fh5enmmv > div.l9j0dhe7.tkr6xdv7 > div.j83agx80.l9j0dhe7.du4w35lb.aovydwv3.do00u71z > div > div > div > div > div > div > div.rj1gh0hx.buofh1pr.j83agx80.cbu4d94t.l9j0dhe7.du4w35lb > div > div.pfnyh3mw > form > div > div.j83agx80.l9j0dhe7.aovydwv3.ni8dbmo4.stjgntxs.nred35xi.n8tt0mok.hyh9befq > div.aovydwv3.j83agx80.buofh1pr.ni8dbmo4.cxgpxx05.sj5x9vvc.qio8uep8.l9j0dhe7 > div.orhb3f3m.h905i5nu.jinzq4gt.emzo65vh.j83agx80.e5nlhep0.ecm0bbzt.h4z51re5.gvyehdmr.mu0vl6fp.msuhji6j.iqy7zqfr.rj1gh0hx.cbu4d94t.buofh1pr.ni8dbmo4.ll8tlv6m.b3i9ofy5.oo9gr5id.flx89l3n.dpja2al7.hedjyaoh > div > div > div > div > div._5rpb > div > div > div > div')
        person_send.send_keys("tranquilo")
        send_message = self.driver.find_element_by_xpath('//*[@id="mount_0_0"]/div/div[1]/div[1]/div[5]/div[1]/div[1]/div[1]/div/div/div/div/div/div/div[2]/div/div[2]/form/div/div[3]/span[2]/div')
        send_message.click() #to send message
        time.sleep(3)
        #close = self.driver.find_element_by_xpath('//*[@id="mount_0_0"]/div/div[1]/div[1]/div[5]/div[1]/div[1]/div[1]/div/div/div/div/div/div/div[1]/div/div/div[3]/span[4]/div/svg')
        close = self.driver.find_element_by_css_selector('#mount_0_0 > div > div:nth-child(1) > div.rq0escxv.l9j0dhe7.du4w35lb > div.poy2od1o.i09qtzwb.n7fi1qx3 > div.l9j0dhe7.fh5enmmv > div.l9j0dhe7.tkr6xdv7 > div.j83agx80.l9j0dhe7.du4w35lb.aovydwv3.do00u71z > div > div > div > div > div > div > div.pfnyh3mw.l9j0dhe7.tkr6xdv7 > div > div > div.j83agx80.bp9cbjyn.pfnyh3mw.cgat1ltu > span:nth-child(4) > div > svg')
        close.click()

def readMessage(self):
    message_received = self.driver.find_element_by_xpath(
        '//*[@id="mount_0_0"]/div/div[1]/div[1]/div[5]/div[1]/div[1]/div[1]/div/div/div/div/div/div/div[2]/div/div[1]/div[1]/div[1]/div/div[3]/div/div[12]/div[4]/div[1]/div/div/div/div[1]/div/div')  # xpath of element
    text_obtained = message_received.text  
    print(str(text_obtained)) #the text from that div

我的问题是:message_received变量仅找到一个带有该xpath的元素。每封邮件都是特定的xpath,这对于收到的每封邮件都是不同的...每封邮件具有不同的div

如何保持从特定人那里收到的最新消息,并将其添加到s列表中以获得每个人的答复?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。