A D
A D

Reputation: 135

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

I am trying to click on right arrow on instagram profile to go through all posts.

This is my current code that navigates to first post and clicks on the right arrow to go to the second 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)

When I try to click on the arrow using the same xpath to go to the third post it does not work because the xpath for the arrow has changed. I would appreciate any help on how to solve this problem. I would like to keep pressing the arrow while posts exist. For reference: 1. I first navigate to 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/ If I try the same

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

again I am not able to go to the third post. How can I select a generic xpath that wil work for the right click arrow after every post? Thank you.

Upvotes: 1

Views: 641

Answers (1)

frianH
frianH

Reputation: 7563

Use .find_element_by_css_selector.

The Next button on Instagram you mean using this selector on all pages:

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

Upvotes: 1

Related Questions