【问题标题】:Python Selenium instagram bot clicking same picturePython Selenium instagram bot 点击同一张图片
【发布时间】:2019-04-01 22:12:55
【问题描述】:

我正在尝试在搜索栏中输入#likeforfollow,这是成功的。然后我尝试使用 for 循环点击每张图片,但它每次都点击同一张图片。我想点击这里的每张图片都是我的代码:

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

【问题讨论】:

  • 这个范围很广。或许将您正在扫描的 HTML 提供给我们,否则我们无法为您提供帮助。
  • @PL200 如果您在 Instagram 的搜索栏中搜索#likeforfollow,我会尝试点击该页面上的每张图片。
  • 您正在循环遍历pic,但随后在该循环中搜索静态 xpath。没有意义。 (也很糟糕的 xpath,使调试几乎不可能)
  • 在哪个网站上,@Noah?堆栈溢出?现在,我猜答案是“Instagram”,但您需要在问题中指定这类内容。

标签: python selenium xpath ui-automation webautomation


【解决方案1】:

例如,检查以下代码:

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

进口:

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

不客气!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-09
    • 2019-02-20
    • 2017-08-28
    • 1970-01-01
    • 2022-07-25
    • 1970-01-01
    • 2016-08-13
    • 1970-01-01
    相关资源
    最近更新 更多