NGuyen
NGuyen

Reputation: 275

Python 3 + Selenium: How to find this element by xpath?

https://www.facebook.com/friends/requests/?fcref=jwl&outgoing=1

I want to click "See more request" in the "friend request sent"

<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>

I used this code but it doesn't work.

driver.find_element_by_xpath("//*[contains(@id, 'outgoing_reqs_pager')]").click()

I got the error:

Message: Element is not clickable at point (371.5, 23.166671752929688). Other element would receive the click:

<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">

How to click it ? Thank you :)

Upvotes: 1

Views: 1261

Answers (1)

Saurabh Gaur
Saurabh Gaur

Reputation: 23835

Actually here you are locating element by using partial id match, so may be this xpath is not unique and return multiple element.

find_element always returns first element by matching locator, may be it's return other element which is overlayed by other element instead of desire element that's why you are in trouble.

Here you should try using link_text to locate desire element by using their text as below :-

driver.find_element_by_link_text("See More Requests").click()

Or using partial_link_text as below :-

driver.find_element_by_partial_link_text("See More Requests").click()

Edited1 :- If you're still getting same exception you need to scroll first to reach that element using exexute_script() then click as below :-

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()

Or if you don't want to scroll, you can click using exexute_script without scrolling as below :-

link = driver.find_element_by_partial_link_text("See More Requests")

#now perform click using javascript
driver.exexute_script("arguments[0].click()", link)

Edited2 :- If you want to click this link inside while loop until it appears, you need to implement WebDriverWait to wait until it present next time as below :-

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

Upvotes: 1

Related Questions