instagram 配置文件帖子视图上右箭头的 xpath 不适用于所有帖子(python、selenium、chromedriver)

xpath for right arrow on instagram profile posts view not working for all posts(python, selenium, chromedriver)

我正在尝试单击 Instagram 个人资料上的向右箭头以浏览所有 posts。

这是我当前的代码,导航到第一个 post 并单击右箭头转到第二个 post:

 driver.get("https://www.instagram.com/leomessi/")
time.sleep(2)
#click on first post
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//body//div[contains(@class,'_2z6nI')]//div//div//div[1]//div[1]//a[1]//div[1]//div[2]"))).click()
time.sleep(2)
#hit next
driver.find_element_by_xpath('/html/body/div[4]/div[1]/div/div/a').click()
time.sleep(2)

当我尝试使用相同的 xpath 单击箭头转到第三个时 post 它不起作用,因为箭头的 xpath 已更改。我将不胜感激有关如何解决此问题的任何帮助。我想在 post 存在时继续按箭头。 供参考: 1. 我首先导航到 https://www.instagram.com/leomessi/ 2. Finding and clicking on the first post results in https://www.instagram.com/p/B-KvtQbil7m/ 3. Clicking on the arrow once results in https://www.instagram.com/p/B9t8tu4KeYK/ 如果我尝试相同的

driver.find_element_by_xpath('/html/body/div[4]/div[1]/div/div/a').click()

我又一次无法进入第三个 post。我怎样才能 select 一个通用的 xpath,它会在每个 post 之后用于右键单击箭头?谢谢。

使用.find_element_by_css_selector.

Instagram 上的 Next 按钮是指在所有页面上使用此选择器:

driver.find_element_by_css_selector('a.coreSpriteRightPaginationArrow').click()