https://www.facebook.com/friends/requests/?fcref=jwl&outgoing=1
我想在“已发送的好友请求”中单击“查看更多请求”
<div id="outgoing_reqs_pager_57b87e5793eb04682598549" class="clearfix mtm uiMorePager stat_elem _646 _52jv">
<div>
<a class="pam uiBoxLightblue _5cz uiMorePagerPrimary" role="button" href="#" ajaxify="/friends/requests/outgoing/more/?page=2&page_size=10&pager_id=outgoing_reqs_pager_57b87e5793eb04682598549" rel="async">See More Requests</a>
<span class="uiMorePagerLoader pam uiBoxLightblue _5cz">
</div>
</div>
我使用了这段代码,但是它不起作用。
driver.find_element_by_xpath("//*[contains(@id, 'outgoing_reqs_pager')]").click()
我得到了错误:
消息:元素在点(371.5,23.166671752929688)不可单击。其他元素将获得点击:
<input aria-owns="js_8" class="_1frb" name="q" value="" autocomplete="off" placeholder="Search Facebook" role="combobox" aria-label="Search" aria-autocomplete="list" aria-expanded="false" aria-controls="js_5" aria-haspopup="true" type="text">
怎么点击呢?谢谢 :)
实际上,您是通过使用部分ID匹配来定位元素的,因此此xpath可能不是唯一的,并且返回多个元素。
find_element
总是通过匹配定位器返回第一个元素,可能是返回了其他元素覆盖的其他元素,而不是欲望元素,这就是您遇到麻烦的原因。
在这里,您应该尝试link_text
通过使用如下文本来查找所需元素:-
driver.find_element_by_link_text("See More Requests").click()
或使用partial_link_text
如下:
driver.find_element_by_partial_link_text("See More Requests").click()
Edited1:-如果仍然遇到相同的异常,则需要先滚动以使用来到达该元素,exexute_script()
然后单击如下所示:-
link = driver.find_element_by_partial_link_text("See More Requests")
#now scroll to reach this link
driver.exexute_script("arguments[0].scrollIntoView()", link)
#now click on this link
link.click()
或者,如果您不想滚动,则可以单击使用exexute_script
而不滚动,如下所示:-
link = driver.find_element_by_partial_link_text("See More Requests")
#now perform click using javascript
driver.exexute_script("arguments[0].click()", link)
Edited2:-如果要在while循环中单击此链接直到它出现,则需要实现WebDriverWait
等到下一次出现,如下所示:-
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
# now find link
link = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "See More Requests")))
#now perform click one of these above using java_script
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句