


Unliked = u-_7\" and = u-_7\" and If there are like buttonsĮxcept StaleElementReferenceException: # We face this stale element reference exception when the element we are interacting is destroyed and then recreated again. """Method that finds all the like buttons and clicks on each one of them, if they are not already clicked (liked).""" Print("Couldn't close the picture, navigating back to Instagram's main page.") # If driver fails to find the close button, it will navigate back to the main page # Closing the picture pop up after having liked the last all pictures of " + _url) # Create has_picture variable to keep track """ Method that likes every picture on an Instagram page.""" Like each one of them: def like_all_pictures(self): """ Method that opens the first picture on an Instagram profile page NoSuchElementException: Then I open the first picture: def open_first_picture(self): Print("Successfully searched for: " + keyword) _element_by_xpath(search_input).send_keys(keyword) """ Method that searches for a username and navigates to nth profile in the results where n corresponds to the index""" It could be used on the "Explore" page or simply on an user's profil page.įirst, to navigate to a profil page from Instagram's main page, I create a xPath for the "SearchBox" and a xPath to the element in the dropdown menu results corresponding to the index. I implemented a function that likes all the picture from an Instagram page.

Heart_icon = driver.find_element_by_xpath(my_xpath)

# Get xpath of heart icon of the 19th post. Return f'xpath=//article/div/section/span/button/span' Return heart icon xpath corresponding to n-post. So you can manage to get the number of the post you desire and then click it: def get_heart_icon_xpath(post_num): The first number near "article" corresponds to the number of the post. The xpath of the icon of the third post is: xpath=//article/div/section/span/button/spanĪnd so on. The xpath of the icon of the second post is: xpath=//article/div/section/span/button/span The xpath of the icon of the first post is: xpath=//button/span After some research I found that the xpath of the heart icon below left the post behaves like that: Now let's talk about possible solutions and them implementations. If you really need using Selenium I also recommend downloading Selenium IDE add-on for Chrome here since it can save you much time, believe me. It would make your bot much simpler, more readable and most of all lighter and faster. First of all, in your case it is recommended to use the official Instagram Api for Python ( documentation here on github).
