3

I'm trying to like and unlike all the post present in an Instagram feed page. I'm able to like a first single post not able to find a way to like multiple posts . I'm using XPath to get an element of that page

comment_like_xpath="//button/span[@class='glyphsSpriteHeart__outline__24__grey_9 u-__7' and @aria-label='Like']"
comment_unlike_xpath="//button/span[@class='glyphsSpriteHeart__filled__24__red_5 u-__7' and @aria-label='Unlike']"

I can click on single first post using this xpath as below

comment_like_elem = wait.until(EC.visibility_of_element_located((By.XPATH,comment_like_xpath))).click()

if I use list of elements present in page I get exception error

comment_like_elem = browser.find_elements_by_xpath(comment_like_xpath)
comment_like_elem_len = len(comment_like_elem)#no  of liked elemets

If I iterate through list of elemensts I get an exception error

for element in  comment_like_elem:   
    element.click()

each elements are

<selenium.webdriver.remote.webelement.WebElement (session="53072af595e97e0c84dfadcabbe3b44e", element="97c695b9-1ae0-4a37-a0fd-5ff5a52a2b1f")>

I get an exception at element.click() as

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (487, 743). Other element would receive the click: ... (Session info: chrome=76.0.3809.132)

element of the like button is as below

<button class="dCJp8 afkep _0mzm-"><span class="glyphsSpriteHeart__outline__24__grey_9 u-__7" aria-label="Like"></span></button>

even I tried uisng this xpath

//*[@id="react-root"]/section/main/section/div[2]/div[1]/div/article[1]/div[2]/section[1]/span[1]/button/span

and iterating through this it gives me a same ElementClickInterceptedException error after liking a single post

code i written to login to page is as bellow

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import ElementClickInterceptedException
from selenium.common.exceptions import TimeoutException, NoSuchElementException, ElementClickInterceptedException
from selenium.webdriver.common.action_chains import ActionChains
options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument('--user-agent=Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1')

browser = webdriver.Chrome('path of chromedriver', options = options)
browser.set_page_load_timeout(30)
wait = WebDriverWait(browser, 30)
try:
    browser.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
except TimeoutException:
    print('continue')
    browser.close()
    exit()
time.sleep(3)
login_elem_usr = browser.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div/div/div/form/div[4]/div/label/input')
login_elem_usr.click()
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='username']"))).send_keys("username")
login_elem_pwd = browser.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div/div/div/form/div[5]/div/label/input')
login_elem_pwd.click()
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='password']"))).send_keys("password")
login_elem_login = browser.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div/div/div/form/div[7]/button/div')
login_elem_login.click()
time.sleep(5)
try:
    login_elem_popup = wait.until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[3]/div/div/div[3]/button[2]")))#.click()
    login_elem_popup.click()
except TimeoutException:
    print("contine")
try:
    filterButtonElement = wait.until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[3]/div/div/div[3]/button[2]")))
    filterButtonElement.click()
except TimeoutException:
    print("contine")

So how can I like all the post present in my feed ? any suggestions will be helpful thanks

deepinside
  • 351
  • 1
  • 5
  • 15
  • Can you post a picture of the website? From what I'm seeing in an Instagram Avengers feed, these buttons are not visible, unless I click the picture first – RegCent Sep 02 '19 at 05:39
  • @RegCent I'm opening page through mobile version page. I have updated code have a look at the code – deepinside Sep 02 '19 at 05:45
  • Seems that your element is behind some loading element, did you tried to wait for the overlapping element to be invisible, also did you tried to scroll to the element? – Spencer Melo Sep 02 '19 at 05:53
  • @SpencerMelo yes I tried both of them delaying and scrolling down the page I get the same error – deepinside Sep 02 '19 at 06:25

1 Answers1

1

Try to send click event by js. Maybe it helps you

comment_like_elem = wait.until(EC.presence_of_all_elements_located((By.XPATH,comment_like_xpath)))
for item in comment_like_elem:
    browser.execute_script("arguments[0].click()", item)
brfh
  • 334
  • 1
  • 12
  • thanks your solution works perfectly. It worked. can u explain me how u did that? – deepinside Sep 02 '19 at 14:22
  • 1
    @deepinside, I think, this post can explain you how the js scripts works in selenium (and this case in particular ) . Gleed to help you. [link](https://stackoverflow.com/questions/11947832/how-to-click-an-element-in-selenium-webdriver-using-javascript) – brfh Sep 03 '19 at 07:36