selenium facebook评论回复

时间:2015-12-08 11:37:51

标签: python xml facebook selenium selenium-webdriver

尝试使用selenium和python回复facebook评论。 我已经能够使用

选择字段了
 find_elements_by_css_selector(".UFIAddCommentInput")

但是我无法使用send_keys方法发布文本。下面是facebook的评论html的简化结构:

<div><input tabindex="-1" name="add_comment_text">
<div class="UFIAddCommentInput _1osb _5yk1"><div class="_5yk2">
<div class="_5yw9"><div class="_5ywb">
<div class="_3br6">Write a comment...</div></div>
<div class="_5ywa">
<div title="Write a comment..." role="combobox"       

 class="_54z"contenteditable="true">
<div data-contents="true">
 <div class="_209g _2vxa">

1 个答案:

答案 0 :(得分:0)

它完美无缺。唯一的问题是,在开始输入新评论之前,每次都要清除div

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

f = webdriver.Firefox()
f.get("https://facebook.com")

# Skipping the logging in and going to a particular post part.

# The selector I used correspond to id of the post and 
# class of the div that contains editable text part
e = f.find_element_by_css_selector("div#u_jsonp_8_q div._54-z")
e.send_keys("bob")
e.send_keys(Keys.ENTER)
e.clear()

e.send_keys("kevin")
e.send_keys(Keys.ENTER)
e.clear()