Codieroot
Codieroot

Reputation: 747

Click on Instagram post using Selenium Python

Hi I am using Selenium to visit a instagram profile and click on the first post. Language I am using is python. As an example, I am trying to open the profile instagram.com/yotta_life and I got the XPATH of first post : //*[@id=\"react-root\"]/section/main/article/div/div[1]/div[1]/div[1]/a/div/div[2]

I can visit the profile in Selenium but error message is showing, Element is not clickable at point (1015, 635)

This is my code :-

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
def login(driver):
    username = "username"  # <username here>
    password = "password"  # <password here>

    # Load page
    driver.get("https://www.instagram.com/accounts/login/")

    # Login
    driver.find_element_by_xpath("//div/input[@name='username']").send_keys(username)
    driver.find_element_by_xpath("//div/input[@name='password']").send_keys(password)
    driver.find_element_by_xpath("//span/button").click()

    widgets_click = WebDriverWait(driver, 500000).until(
                    EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div/div[2]/div/div/button"))
                )
    widgets_click.click()

def scrape(driver, account):

    # Load account page
    driver.get("https://www.instagram.com/{0}/".format(account))

    # This is the first post click which is not working
    postdivrow_click = WebDriverWait(driver, 500000).until(
                    EC.element_to_be_clickable((By.XPATH, "//*[@id=\"react-root\"]/section/main/article/div/div[1]/div[1]/div[1]/a/div/div[2]"))
                )
    postdivrow_click.click()

if __name__ == "__main__":

    chromedriver = "Documents/chromedriver"
    driver = webdriver.Chrome(chromedriver)
    try:
        login(driver)
        followers = scrape(driver, "yotta_life")
    finally:
        driver.quit()

Problem here is that element is not clickable. I think, I am not using correct XPATH. What is the recommended way to do it.

Upvotes: 1

Views: 2271

Answers (1)

Codieroot
Codieroot

Reputation: 747

Here, I have to use

driver.execute_script("window.scrollTo(0, Y)")

Y is the height, to scroll down the page.

Upvotes: 1

Related Questions