Patriots299
Patriots299

Reputation: 365

Python/Selenium: Identify all Instagram hearts and 'Like' them

Program is supposed to detect all the hearts in Instagram and then give a 'Like'. I am aware there's an Instagram API, but trying to implement with Selenium for educational pruposes. Additionally, I am using Chrome.

This is what I tried so far:

# scroll down to the bottom of the page
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

driver.maximize_window()

# find all heart links
hearts = driver.find_elements_by_xpath("//button[@class='dCJp8 afkep coreSpriteHeartOpen _0mzm-']")

for i in range(len(hearts)):
    hearts[i].click()
    sleep(3)

Error:

selenium.common.exceptions.WebDriverException: Message: unknown error: Element <button class="dCJp8 afkep coreSpriteHeartOpen _0mzm-">...</button> is not clickable at point (192, 20). Other element would receive the click: <div class="                  Igw0E   rBNOH        eGOV_     ybXk5    _4EzTm                                                                                                              ">...</div>
  (Session info: chrome=71.0.3578.98)

From what I am able to follow, the element my program is pointing at, does not seem correct. This is what I am using:

enter image description here

I have also tried both the upper and child span elements. Does anyone has any other idea of what could be wrong? Thanks in advance.

Edit: Resolved utilizing Actionchain(). Before attempting to click the element, I added code to move to it first.

hearts = driver.find_elements_by_xpath("//span[@class='fr66n']")

for h in range(len(hearts)):
    ActionChains(driver).move_to_element(hearts[h]).click(hearts[h]).perform()
    print(hearts[h])

Upvotes: 0

Views: 882

Answers (1)

LilMia
LilMia

Reputation: 1

This should work:

likeButtonPath= 'section.ltpMr.Slqrh > span.fr66n > button > div > span > svg[aria-label="Like"]'
elements= LOGINPG.find_elements_by_css_selector(likeButtonPath)

for element in elements:
  LOGINPG.execute_script('arguments[0].scrollIntoView({behavior: "smooth", block: "center", inline: "nearest"});', element)
  element.click()

Upvotes: 0

Related Questions