点击使用Selenium Python的Instagram帖子

时间:2018-01-19 23:19:00

标签: python selenium headless

您好我正在使用Selenium访问Instagram个人资料并点击第一篇文章。我使用的语言是python。例如,我正在尝试打开配置文件instagram.com/yotta_life,我获得了第一篇文章的XPATH://*[@id=\"react-root\"]/section/main/article/div/div[1]/div[1]/div[1]/a/div/div[2]

我可以访问Selenium中的个人资料,但会显示错误消息Element is not clickable at point (1015, 635)

这是我的代码: -

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()

这里的问题是该元素不可点击。我想,我没有使用正确的XPATH。建议的方法是什么。

1 个答案:

答案 0 :(得分:0)

在这里,我必须使用

prop.test

Y是向下滚动页面的高度。