使用Python在Facebook上发送消息

时间:2018-01-18 23:20:02

标签: python facebook message facebook-messenger facebook-messages

尝试使用Python在Facebook上发送消息。所有步骤都有效,直到将文本放入textarea发送。获取错误代码:

  

无法找到元素:{"方法":" css选择器","选择器":" div   textarea.uiTextareaNoResize"}

以下是整个代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import sched, time, timeit
import random
import getpass
import sys

driver = webdriver.Chrome('C:\path\chromedriver.exe')
driver.get('https://www.facebook.com/')

username_box = driver.find_element_by_id('email')
username_box.send_keys("USEREMAIL")
passElem = driver.find_element_by_id("pass")
passElem.send_keys("USERPASS")
passElem.send_keys(Keys.RETURN)
userTargetUrl = "https://www.facebook.com/messages/t/" + "USERTARGET NAME"
driver.get(userTargetUrl)

elem = driver.find_element_by_css_selector("div textarea.uiTextareaNoResize")

elem.send_keys("Test")
elem.send_keys(Keys.RETURN)
driver.find_element_by_id("u_0_t").click()

此处出现错误:

elem = driver.find_element_by_css_selector("div textarea.uiTextareaNoResize")

elem.send_keys("Test")
elem.send_keys(Keys.RETURN)
driver.find_element_by_id("u_0_t").click()

在控制台上收到此消息:

selenium.common.exceptions.NoSuchElementException: 
Message: no such element: 
Unable to locate element: {"method":"css selector","selector":"div textarea.uiTextareaNoResize"}
      (Session info: chrome=63.0.3239.132)
      (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.15063 x86_64)

此处的代码失败:

elem = driver.find_element_by_css_selector("div textarea.uiTextareaNoResize")

In this picture you can see where I want my message to be written.

1 个答案:

答案 0 :(得分:0)

您必须设置一个变量来存储graph.request响应,然后打印它:

token = 'access_token'
graph = facebook.GraphAPI(access_token=token, version="2.11")
response = graph.request('search', {'q': 'Mark Zuckerberg', 'type': 'user'})
print(response)

如果您有access_token值,也无需通过Selenium登录。