selenium webdriver - Threads Follower Button and Followers are not scraping - Stack Overflow

admin2025-04-16  6

this code of mine is not clicking on the followers though i tried every xpath

can Someone help me out

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdrivermon.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from seleniummon.exceptions import NoSuchElementException, TimeoutException

def setup_driver():
    options = Options()
    options.add_argument('--log-level=3')
    options.add_experimental_option('excludeSwitches', ['enable-logging', 'enable-automation'])
    options.add_experimental_option('useAutomationExtension', False)
    return webdriver.Chrome(options=options)

def login_to_threads(driver, wait):
    driver.get('')
    try:
        # Click Instagram login button
        insta_button = wait.until(EC.element_to_be_clickable(
            (By.XPATH, "//span[contains(text(), 'Continue with Instagram')]")))
        insta_button.click()
        
        # Read credentials
        with open('credentials.txt', 'r') as f:
            username, password = [line.strip() for line in f.readlines()[:2]]
        
        # Login
        username_field = wait.until(EC.presence_of_element_located((By.NAME, 'username')))
        password_field = driver.find_element(By.NAME, 'password')
        username_field.send_keys(username)
        password_field.send_keys(password)
        driver.find_element(By.XPATH, "//button[@type='submit']").click()
        
        # Handle post-login popups
        for button_text in ['Save info', 'Not Now']:
            try:
                wait.until(EC.element_to_be_clickable(
                    (By.XPATH, f"//div[contains(text(), '{button_text}')]"))).click()
            except TimeoutException:
                continue
        return True
    except Exception as e:
        print(f"Login error: {str(e)}")
        return False

def get_followers_button_xpath():
    return By.XPATH, "//div[contains(@class, 'x6s0dn4') and contains(@class, 'x78zum5')]//span[contains(text(), 'followers')]/ancestor::div[contains(@class, 'x6s0dn4')]"

def click_followers_button(driver, wait):
    try:
        locator = get_followers_button_xpath()
        followers_button = wait.until(EC.element_to_be_clickable(locator))
        
        # Scroll into view and click using JavaScript
        driver.execute_script("arguments[0].scrollIntoView({behavior: 'smooth', block: 'center'});", followers_button)
        driver.execute_script("arguments[0].click();", followers_button)
        print("Successfully clicked followers button")
        return True
    except Exception as e:
        print(f"Failed to click followers button: {str(e)}")
        return False

def extract_visible_followers(driver):
    try:
        return [element.text.strip('@') for element in driver.find_elements(
            By.XPATH, "//div[@role='dialog']//a[contains(@href, '/@')]//span[contains(text(), '@')]"
        ) if element.text.strip()]
    except NoSuchElementException:
        return []

def scroll_followers_list(driver):
    try:
        scroll_container = driver.find_element(
            By.XPATH, "//div[@role='dialog']//div[@class='x1qjc9v5 x1q0q8m5 x1qhh985 xu3j5b3 xcfux6l x26u7qi xm0m39n x13fuv20 x972fbf x1ey2m1c x9f619 x78zum5 x1q0g3np x1iyjqo2 xs83m0k x1qughib']")
        driver.execute_script(
            "arguments[0].scrollTop = arguments[0].scrollHeight", 
            scroll_container
        )
        time.sleep(1.5)
    except NoSuchElementException:
        print("Scroll container not found")

def collect_followers(driver, wait, limit=25):
    all_followers = []
    seen = set()
    retries = 0
    
    while len(all_followers) < limit and retries < 5:
        current_batch = extract_visible_followers(driver)
        new_followers = [f for f in current_batch if f not in seen]
        
        if not new_followers:
            retries += 1
            time.sleep(1)
            continue
            
        retries = 0
        seen.update(new_followers)
        all_followers.extend(new_followers)
        print(f"Collected {len(all_followers)}/{limit} followers")
        scroll_followers_list(driver)
    
    return all_followers[:limit]

def get_followers(driver, wait, target_username):
    driver.get(f'/@{target_username}')
    
    # Wait for profile to fully load
    try:
        wait.until(EC.presence_of_element_located(
            (By.XPATH, "//h1[contains(@class, 'x1heor9g')]")))
    except TimeoutException:
        print("Profile failed to load")
        return []

    if not click_followers_button(driver, wait):
        return []

    # Wait for followers dialog to appear
    try:
        wait.until(EC.presence_of_element_located(
            (By.XPATH, "//div[@role='dialog' and contains(.//span, 'Followers')]")))
    except TimeoutException:
        print("Followers dialog didn't open")
        return []

    return collect_followers(driver, wait)

def main():
    driver = setup_driver()
    wait = WebDriverWait(driver, 15)
    
    try:
        if login_to_threads(driver, wait):
            followers = get_followers(driver, wait, "aabid_een")
            print("\nFinal followers list:")
            for idx, user in enumerate(followers, 1):
                print(f"{idx}. @{user}")
    finally:
        driver.quit()

if __name__ == "__main__":
    main()

I am Trying to scrape Threads Followers but all tries in vain Tried this code my aim is that my code first login to threads and then go to the target user and then click on the followers button and then scrape its followers names

转载请注明原文地址:http://anycun.com/QandA/1744811184a87947.html