Instagram 关注

问题描述 投票:0回答:1

enter image description here

如您所见,应用程序会找到目标配置文件。但是,它无法点击关注按钮,也无法向下滚动列表。

我得到的错误:

C:\Users\Admin\Downloads\Compressed\Instagram-following-bot-main\Instagram-following-bot-main\venv\Scripts\python.exe C:\Users\Admin\Downloads\Compressed\Instagram-following-bot-main\Instagram-following-bot-main\main.py 
Traceback (most recent call last):
  File "C:\Users\Admin\Downloads\Compressed\Instagram-following-bot-main\Instagram-following-bot-main\main.py", line 95, in <module>
    bot.find_followers()
  File "C:\Users\Admin\Downloads\Compressed\Instagram-following-bot-main\Instagram-following-bot-main\main.py", line 53, in find_followers
    EC.visibility_of_element_located((By.XPATH, "//div[@role='dialog']//div[@class='isgrP']"))
  File "C:\Users\Admin\Downloads\Compressed\Instagram-following-bot-main\Instagram-following-bot-main\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 


Process finished with exit code 1


    
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import os

CHROME_DRIVER_PATH = r"C:\Users\Admin\Downloads\Compressed\Instagram-following-bot-main\Instagram-following-bot-main\chromedriver.exe"

# Set Instagram username and password as environment variables
os.environ["xxxxxx"] = "xxxxx"
os.environ["xxxxx"] = "xxxx"

INSTA_USERNAME = os.environ.get("xxxxx")
INSTA_PASSWORD = os.environ.get("xxxxxx")
SIMILAR_ACCOUNT = "xaricdeheyat"
INSTA_URL = "https://www.instagram.com/accounts/login/"
CHEF_STEPS_URL = "https://www.instagram.com/xaricdeheyat/"


class InstaFollower:
    def __init__(self, path):
        self.driver = webdriver.Chrome(executable_path=path)


    def login(self):
        self.driver.get(url=INSTA_URL)
        time.sleep(3)
        username = self.driver.find_element(By.NAME, value="username")
        username.send_keys(INSTA_USERNAME)
        time.sleep(2)
        password = self.driver.find_element(By.NAME, value="password")
        password.send_keys(INSTA_PASSWORD)
        time.sleep(2)
        password.send_keys(Keys.ENTER)
        time.sleep(3)

    def find_followers(self):
        time.sleep(5)
        self.driver.get(CHEF_STEPS_URL)
        time.sleep(3)
        followers = self.driver.find_element(By.PARTIAL_LINK_TEXT, value="followers")
        time.sleep(2)
        followers.click()
        time.sleep(2)

        # Wait for the followers list to be visible
        followers_list = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located((By.XPATH, "//div[@role='dialog']//div[@class='isgrP']"))
        )

        # Scroll through followers list
        followers_count = len(followers_list.find_elements(By.TAG_NAME, "li"))
        print(f"Total followers: {followers_count}")

        actions = ActionChains(self.driver)
        while True:
            followers_list.click()
            actions.key_down(Keys.SPACE).key_up(Keys.SPACE).perform()
            new_count = len(followers_list.find_elements(By.TAG_NAME, "li"))
            print(f"Scrolled to {new_count} followers")

            if new_count >= followers_count:
                break
            followers_count = new_count
            time.sleep(1)

        print("Finished scrolling")

    def follow(self):
        try:
            # Locate all the buttons for following
            buttons = self.driver.find_elements(By.XPATH, "//button[contains(text(), 'Follow')]")

            # Loop through each button and scroll into view
            for button in buttons:
                # Scroll the button into view
                self.driver.execute_script("arguments[0].scrollIntoView();", button)
                time.sleep(1)  # Wait for the button to scroll into view

                # Click the button to follow
                button.click()
                time.sleep(3)  # Wait for the follow action to complete

        except Exception as e:
            print(e)


bot = InstaFollower(CHROME_DRIVER_PATH)
bot.login()
bot.find_followers()
bot.follow()
python selenium-webdriver pycharm bots instagram
1个回答
0
投票
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait_until = WebDriverWait(driver, 20)  # Increase the timeout value
element = wait_until.until(EC.presence_of_element_located((By.XPATH, 'your_xpath_here')))

文档:https://selenium-python.readthedocs.io/waits.html

© www.soinside.com 2019 - 2024. All rights reserved.