简体   繁体   中英

Python Selenium instagram bot clicking same picture

I am trying to type #likeforfollow in search bar, which was successful. Then I tried to use a for loop to click on every picture but it just clicks on same picture every time. I want to click every picture here is my code:

for pic in driver.find_elements_by_class_name('eLAPa'):
    driver.find_element_by_xpath('''//*[@id="react-root"]/section/main/article/div[1]/div/div/div[1]/div[3]/a/div''').click()
    driver.find_element_by_xpath("""/html/body/div[3]/div/button""").click()

As an example, check this code:

browser = webdriver.Chrome()

browser.get('https://www.instagram.com/accounts/login/')
wait = WebDriverWait(browser, 5)
wait.until(EC.presence_of_element_located((By.XPATH, '//input[@name="username"]'))).send_keys('USERNAME')
browser.find_element_by_xpath('//input[@type="password"]').send_keys('PASSWORD')
browser.find_element_by_xpath('//button[contains(text(), "Log in")]').click()

browser.get('https://www.instagram.com/explore/tags/likeforfollow/')
for i in range(10):
    browser.execute_script('window.scrollTo(0, document.body.scrollHeight);')
    time.sleep(1)

pics = wait.until(EC.presence_of_all_elements_located((By.XPATH,     '//a[contains(@href, "tagged=likeforfollow")]')))
links = [pic.get_attribute("href") for pic in pics]
for link in links:
    browser.get(link)
    wait.until(EC.presence_of_element_located((By.XPATH, '//button[contains(@class, "coreSpriteHeartOpen")]'))).click()

browser.close()

Imports:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time

You are welcome!

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