I'm trying to clear my Instagram chat from messages. I wrote the following code for this, but when I try to delete the second message, a NoSuchElementException on the threeDots initialization line appears, although the xpath itself is correctly written.
if len(positionsArray) > 1:
for i in range(len(chatMessagesArray)):
# Click on three dots (else button)
threeDotsXpath = browser.find_element_by_xpath(f"/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[1]/div/div/div[{positionsArray[len(positionsArray) - 1] + 1}]/div[2]/div/div/div/button[1]").click()
# Deleting message and confirmation of it
deleteMessageDiv = browser.find_element_by_xpath(f"/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[1]/div/div/div[{positionsArray[len(positionsArray) - 1] + 1}]/div[2]/div/div/div/div[2]/div/div[2]/div[4]/button").click()
browser.find_element_by_xpath("/html/body/div[6]/div/div/div/div[2]/button[1]").click()
positionsArray.remove(positionsArray[len(positionsArray) - 1])
Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[1]/div/div/div[2]/div[2]/div/div/div/button[1]"}