简体   繁体   English

使用 Python 和 Selenium 向 Facebook 中的朋友发送消息

[英]Using Python & Selenium to send message to friend in Facebook

I am currently writing a script in Python to log in on facebook and I want to send message to a friend.我目前正在用 Python 编写一个脚本来登录 facebook,我想向朋友发送消息。 The script logs in to my facebook, and it manages to find my friend, but the message is not sent.该脚本登录到我的 facebook,它设法找到了我的朋友,但没有发送消息。 I'm not 100% sure, but I think the problem is in the div tag/CSS on the text area (I commented out that piece of code).我不是 100% 确定,但我认为问题出在文本区域上的 div 标签/CSS(我注释掉了那段代码)。

Screenshot: Text doesn't appear here屏幕截图:此处未显示文本

Here is my code:这是我的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

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(USERPASSWORD)
passElem.send_keys(Keys.RETURN)
userTargetUrl = "https://www.facebook.com/messages/t/" + "USERTAGET"
driver.get(userTargetUrl)

//The problem is here I think
    elem = driver.find_element_by_css_selector("div textarea.uiTextareaNoResize")


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

The error code i get is:我得到的错误代码是:

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)

I solved this problem on RU SO )))我在RU SO上解决了这个问题 )))

Briefly in steps:简要分步骤:

  1. Making the library import进行库导入

    from selenium.webdriver.common.action_chains import ActionChains

  2. Visited the site访问了网站

  3. Logged in已登录

  4. You have a friend list, you clicked on your friend's icon and a chat window has opened.您有一个朋友列表,您单击了朋友的图标并打开了一个聊天窗口。

now what to do?))现在该怎么办?))

  1. You need to place the cursor in the input line, in which the text is not written) Use for this .click () , you can use any of your XPath, but here is a shorter one))您需要将光标放在输入行中,其中未写入文本)为此使用.click () ,您可以使用任何您的 XPath,但这里是一个较短的))

    driver.find_element_by_xpath("//*[@data-editor]").click()

  2. After pressing the .click () method, the cursor is placed in the text input area.按下.click ()方法后,光标放置在文本输入区。 And now we need to enter the text, but use .send_keys() not with the pointer to the element, as you tried, but separately as an action (this is what the imported library is for) actions = ActionChains(driver) actions.send_keys('HI') actions.perform()现在我们需要输入文本,但是使用.send_keys()不是像您尝试的那样与指向元素的指针一起使用,而是单独作为一个动作(这是导入的库的用途) actions = ActionChains(driver) actions.send_keys('HI') actions.perform()

Woo a la))))哇啦))))

在此处输入图片说明

Well, after that .click() on the send icon or press Enter)))))好吧,之后.click()在发送图标上或按 Enter)))))

Try to find the element by name, id, text or any other unique parameter - it would be better (they sometimes change the design and all xpaths and css-selectors become useless).尝试按名称、id、文本或任何其他唯一参数查找元素 - 会更好(它们有时会更改设计并且所有 xpath 和 css 选择器都变得无用)。

I made bold what I'd use:我大胆地使用了:

 <div aria-autocomplete="list" aria-controls="js_7" aria-describedby="js_0" aria-expanded="false" <b>aria-label="Введите сообщение..."</b> class="notranslate _5rpu" role="combobox" spellcheck="true" style="outline: medium none currentcolor; -moz-user-select: text; white-space: pre-wrap; overflow-wrap: break-word;" <b>tabindex="9999"<b> <b>id="js_d"</b> contenteditable="true"><div data-contents="true">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM