我有一个帖子列表(生日祝福)我想要的。 Facebook用一个URL显示它们,所以我用Selenium打开它,向下滚动显示帖子(点击"显示更多" div)
当我尝试收集类似按钮并单击它们时,我遇到了问题。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
usr = ""
pwd = ""
driver = webdriver.Firefox()
driver.implicitly_wait(10) # seconds
driver.get("url of birthday posts")
# or you can use Chrome(executable_path="/usr/bin/chromedriver")
assert "Facebook" in driver.title
elem = driver.find_element_by_id("email")
elem.send_keys(usr)
elem = driver.find_element_by_id("pass")
elem.send_keys(pwd)
elem.send_keys(Keys.RETURN)
while True:
try:
scroll = WebDriverWait(driver, 4).until(EC.visibility_of_element_located((By.XPATH, "//*[@id='u_0_9']/li[6]/a")))
except TimeoutException:
break
scroll.click()
buttons = driver.find_elements_by_xpath("//a[text()='Mi piace']")
for element in buttons:
element.click()
它不会返回任何错误,但它也无法正常工作! 如果不是指向/输入[1],我会更深入地指向带有'喜欢'的标签。文本返回'元素不可点击'。隐含的等待并没有解决这个问题。
这里是带有类似按钮,评论等的div的HTML(可能更容易在Facebook上检查):
<div><form rel="async" class="commentable_item" method="post" data-testid="feedback_form" data-ft="{"tn":"]"}" action="/ajax/ufi/modify.php" id="u_0_c" onsubmit="return window.Event && Event.__inlineSubmit && Event.__inlineSubmit(this,event)"><input type="hidden" name="charset_test" value="€,´,€,´,水,Д,Є"><input type="hidden" name="fb_dtsg" value="AQGBrIHV7nty:AQEeHD-RcWyK" autocomplete="off"><input type="hidden" autocomplete="off" name="ft_ent_identifier" value="10209101309310163"><input type="hidden" autocomplete="off" name="data_only_response" value="1"><div class="_sa_ _5vsi _ca7"><div class="_37uu"><div data-reactroot=""><div class="_3399 _a7s clearfix"><div class="_524d"><div class="_42nr"><span><div class="_khz"><a aria-pressed="false" class="UFILikeLink _4x9- _4x9_ _48-k" data-testid="fb-ufi-likelink" href="#" role="button" tabindex="-1"><!-- react-text: 8 -->Mi piace<!-- /react-text --></a><div class="_2r6l accessible_elem"><div class="_1oxj uiLayer hidden_elem" style="left: 194px; top: 319px;"><div class="_49v-"><div data-reactroot="" height="52" class="_1oxk"><div class="_iu-" aria-label="Reazioni" role="toolbar"><span aria-pressed="false" aria-label="Mi piace" class="_iuw" href="#" role="button" tabindex="0"><div class="_39m" data-reaction="1"><div class="_39n"><i class="_3j7l _iuz"></i><div class="_d6l"><div class="_4sm1">Mi piace</div></div></div></div></span><span aria-pressed="false" aria-label="Love" class="_iuw" href="#" role="button" tabindex="-1"><div class="_39m" data-reaction="2"><div class="_39n"><i class="_3j7m _iuz"></i><div class="_d6l"><div class="_4sm1">Love</div></div></div></div></span><span aria-pressed="false" aria-label="Ahah" class="_iuw" href="#" role="button" tabindex="-1"><div class="_39m" data-reaction="4"><div class="_39n"><i class="_3j7o _iuz"></i><div class="_d6l"><div class="_4sm1">Ahah</div></div></div></div></span><span aria-pressed="false" aria-label="Wow" class="_iuw" href="#" role="button" tabindex="-1"><div class="_39m" data-reaction="3"><div class="_39n"><i class="_3j7n _iuz"></i><div class="_d6l"><div class="_4sm1">Wow</div></div></div></div></span><span aria-pressed="false" aria-label="Sigh" class="_iuw" href="#" role="button" tabindex="-1"><div class="_39m" data-reaction="7"><div class="_39n"><i class="_3j7r _iuz"></i><div class="_d6l"><div class="_4sm1">Sigh</div></div></div></div></span><span aria-pressed="false" aria-label="Grrr" class="_iuw" href="#" role="button" tabindex="-1"><div class="_39m" data-reaction="8"><div class="_39n"><i class="_3j7q _iuz"></i><div class="_d6l"><div class="_4sm1">Grrr</div></div></div></div></span></div><div class="_41nt" style="height: 52px;"></div></div></div></div></div></div></span><span><a class="comment_link _5yxe" role="button" href="#" title="Lascia un commento" data-ft="{ "tn": "S", "type": 24 }">Commenta</a></span></div></div></div></div></div></div><div class="uiUfi UFIContainer _5pc9 _5vsj _5v9k" id="u_0_v"><div data-reactroot="" class="UFIList"><h6 class="accessible_elem">Commenti</h6><div></div><div id="addComment_10209101309310163" class="UFIRow _4oep UFIAddComment UFIAddCommentWithPhotoAttacher _4204 _2o9m" data-ft="{"tn":"["}"><div class="UFIMentionsInputWrap UFIStickersEnabledInput clearfix" direction="left"><div class="_ohe lfloat"><div class="UFIReplyActorPhotoWrapper img _8o _8r UFIImageBlockImage"><img alt="Enrico Bergamini" class="img UFIActorImage _54ru img" src="https://scontent-mxp1-1.xx.fbcdn.net/hprofile-ash2/v/t1.0-1/c0.3.100.100/p100x100/1901608_10203597124548984_73184209618736027_n.jpg?oh=c60782b3357d58eb0f66bed8c63c3510&oe=5774B23F"></div></div><div class=""><div class="UFIImageBlockContent _42ef _8u"><div class="UFICommentContainer"><div class="UFIInputContainer"><div><div class="UFIAddCommentInput _1osb _1osc"><input class="_1osa mentionsHidden" name="add_comment_text"><!-- react-text: 16 -->Scrivi un commento...<!-- /react-text --></div></div><div class="UFICommentAttachmentButtons clearfix"><div class="_r1a UFIPhotoAttachLinkWrapper _m" data-hover="tooltip" data-tooltip-alignh="center" data-tooltip-content="Allega una foto"><span class=" UFICommentPhotoIcon"><input type="file" accept="image/*" aria-label="Allega una foto" class="_n" name="file" title="Allega una foto"></span></div><a class="_r1a UFICommentStickerButton" data-hover="tooltip" data-tooltip-alignh="center" data-tooltip-content="Invia un adesivo" href="#" id="js_8"><div tabindex="0" class="UFICommentStickerIcon"></div></a></div></div></div></div></div></div></div></div></div></form></div>
我没有编程方面的专家,我觉得我没有指向正确的xpath事件监听器。我如何理解可点击的xpath的位置(通常在DOM中,但也以这种形式)?
答案 0 :(得分:0)
查找和单击按钮的解决方案是使用非常舒适的Selenium的Javascript执行程序注入Javascript:
driver.execute_script("var elems = document.getElementsByClassName('UFILikeLink _4x9- _4x9_ _48-k');for(var i= 0;i<elems.length;i++){elems[i].click();}");
答案 1 :(得分:0)
可以按如下方式提取Facebook上的“赞”按钮:
driver.find_elements(By.CSS_SELECTOR, ".UFILikeLink._4x9-._4x9_._48-k")
为避免讨厌已经喜欢的帖子,您可以使用:
for x in range(0,len(like)):
liked = like[x].get_attribute("data-testid")
if liked == "fb-ufi-likelink":
like[x].click()