So as you know, when you open a post on Instagram on Desktop, it will show "Liked by someone and 400 others". I am trying to click the "400 others" button using selenium, so that I could then go on to scrape the followers' names. I retreived the Xpath of the button as /html/body/div1/section/main/div/div/article/div[2]/section[2]/div/div[2]/button
However, an error arises, which is the StaleElementReferenceException. Below are my codes:
likes = driver.find_element_by_xpath("/html/body/div[1]/section/main/div/div/article/div[2]/section[2]/div/div[2]/button")
actions.move_to_element(likes).perform()
likes.click()
The error happens on the 2nd line, actions.move_to_element(likes).perform()
I wonder whether anyone knows how I can fix it.
ElementNotInteractableException occurs when an element is found, but you can't be interacted with because for example element is hide by another element or is not displayed for some reason and is not clickable anymore. Also you didn't say on which exactly step it failed with StaleElementReferenceException. Anyway maybe using Wait until an element is visible / clickable could help in this case. First solution could be refresh the page and try again for the same element:
driver.refresh()
Second solution could be wait for the element till it gets available:
from selenium.webdriver.support import expected_conditions as EC
likes = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/section/main/div/div/article/div[2]/section[2]/div/div[2]/button")))
likes.click()
Hope it will help you!