如何从"facebook消息"获得接收到的消息.页面使用硒? [英] How to obtain received message from "facebook messages" page using selenium?

查看:95
本文介绍了如何从"facebook消息"获得接收到的消息.页面使用硒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个Python程序,该程序将在发短信后从Facebook消息页面(类似于facebook Messenger)中提取某人的最后一条消息,并将其打印在Python IDE中.

I am trying to write a Python program which will fetch the last message from a person from facebook messages page(similar to facebook messenger) as soon as he texts and print it in a Python IDE.

我正在使用Selenium打开页面并转到希望接收消息的人的文本框.我不知道如何从那里继续,因为每次新文本到达新消息的xpath,selector等时,文字更改,即使我使用了Expected_conditions,我应该怎么做?元素本身在变化.如果我可以获取元素id或xpath或其他方式,则可以轻松地使用gettext()方法获取文本.

I am using Selenium to open the page and get to the textbox of the person from whom I am expecting the message.I have no idea how to proceed from there because everytime the new text comes the xpath,selector etc of the new text changes and even if I use expected_conditions, on what should I? The element itself is changing.If I could get the element id or xpath or otherwise, I could easily use gettext() methods to obtain the text.

下面是我的最小代码,用于到达我希望收到消息的那个人的文本框-

Below is my minimal code, for reaching upto the textbox of the guy from whom I am expecting messages-

from selenium import webdriver 

usr = "x******@gmail.com" 
pwd = "y*****"

opt = webdriver.ChromeOptions()
opt.add_experimental_option("prefs",{"profile.default_content_setting_values.notifications":2}) # Block_notifications
driver = webdriver.Chrome('C:\Chromedriver\chromedriver.exe',chrome_options=opt)

driver.get('https://www.facebook.com/') 
username_box = driver.find_element_by_id('email') 
username_box.send_keys(usr) 
password_box = driver.find_element_by_id('pass') 
password_box.send_keys(pwd) 
login_box = driver.find_element_by_id('u_0_b') 
login_box.click()
driver.get("https://www.facebook.com/messages/t/userid") 
textbox = driver.find_element_by_xpath(r"""//*[@id="js_i"]/div/div/div""")

如何从这里继续,以便在他这样做时得到消息?

How to proceed from here, to get the message when he does?

推荐答案

按照以下步骤解决您的问题.

Your problem will be solved by follwing below steps.

  1. 切换到框架driver.switch_to_frame("//iframe[contains(@src,'userid')]")(注意:此处的用户ID "是聊天用户名.)
  2. 使用此XPath //div[@data-hover='tooltip']/div[@role='img']来识别facebook表情符号或GIF图像.
  3. 使用此XPath //div[@data-hover='tooltip']/div/span读取聊天文本.
  1. Switch to frame driver.switch_to_frame("//iframe[contains(@src,'userid')]") (Note : Here 'userid' is chat username.)
  2. Use this XPath //div[@data-hover='tooltip']/div[@role='img'] to identify the facebook smilies or GIF images.
  3. Use this XPath //div[@data-hover='tooltip']/div/span to read the chat text.

这篇关于如何从"facebook消息"获得接收到的消息.页面使用硒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆