简体   繁体   中英

Python Selenium: Click Instagram next post button

I'm creating an Instagram bot but cannot figure out how to navigate to the next post.
Here is what I tried

#Attempt 1
next_button = driver.find_element_by_class_name('wpO6b  ')
next_button.click()

#Attempt 2 
_next = driver.find_element_by_class_name('coreSpriteRightPaginationArrow').click()

Neither of two worked and I get a NoSuchElementException or ElementClickInterceptedException . What corrections do I need to make here?

This is the button I'm trying to click(to get to the next post)

在此处输入图像描述

I have checked your class name coreSpriteRightPaginationArrow and i couldn't find any element with that exact class name. But I saw the class name partially. So it might help if you try with XPath contains as shown below.

//div[contains(@class,'coreSpriteRight')]

another xpath using class wpO6b. there are 10 elements with same class name so filtered using @aria-label='Next'

//button[@class='wpO6b  ']//*[@aria-label='Next']

Try these and let me know if it works.

I have tried below code and it's clicking next button for 10 times

import time
from selenium import webdriver
from selenium.webdriver.common.by import By

if __name__ == '__main__':
    driver = webdriver.Chrome('/Users/yosuvaarulanthu/node_modules/chromedriver/lib/chromedriver/chromedriver')  # Optional argument, if not specified will search path.
    driver.maximize_window()
    driver.implicitly_wait(15)
    
    driver.get("https://www.instagram.com/instagram/");
    time.sleep(2)
    driver.find_element(By.XPATH,"//button[text()='Accept All']").click();
    time.sleep(2)
    #driver.find_element(By.XPATH,"//button[text()='Log in']").click();
    driver.find_element(By.NAME,"username").send_keys('username')
    driver.find_element(By.NAME,"password").send_keys('password')
    driver.find_element(By.XPATH,"//div[text()='Log In']").click();
    driver.find_element(By.XPATH,"//button[text()='Not now']").click();

    driver.find_element(By.XPATH,"//button[text()='Not Now']").click();
    #it open Instagram page and clicks 1st post and then it will click next post button for the specified range
    driver.get("https://www.instagram.com/instagram/");
    driver.find_element(By.XPATH,"//div[@class='v1Nh3 kIKUG  _bz0w']").click();
    

    for page in range(1,10):
        driver.find_element(By.XPATH,"//button[@class='wpO6b  ']//*[@aria-label='Next']" ).click();
        time.sleep(2)
    driver.quit() 

As you can see on the picture below, the wp06b button is inside a lot of divs, in that case you might need to give Selenium that same path of divs to be able to access the button or give it a XPath .

图片1

It's not the most optimized but should work fine.

driver.FindElement(By.XPath("(.//*[normalize-space(text()) and normalize-space(.)='© 2022 Instagram from Meta'])[1]/following::*[name()='svg'][2]")).Click();

EDIT : Note that the XPath leads to a svg, so basically we are clicking on the svg element itself, not in the button.

As you can see, the next post right arrow button element locator is changing between the first post to other posts next page button.
In case of the first post you should use this locator:

//div[contains(@class,'coreSpriteRight')]

While for all the other posts you should use this locator

//a[contains(@class,'coreSpriteRight')]

The second element //a[contains(@class,'coreSpriteRight')] will also present on the first post page as well, however this element is not clickable there, it is enabled and can be clicked on non-first pages only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM