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

使用Selenium登录Instagram提示「密码错误」的问题求助

问题

编写了如下Python代码尝试登录Instagram,但始终提示“密码错误”,实际密码是正确的。不仅脚本无法登录,通过Selenium驱动打开的Chrome窗口手动登录也失败。尝试了多种selenium-stealth的配置方式,结果均相同,但使用普通Chrome窗口登录一切正常。代码如下:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium_stealth import stealth
import random

username = '***'
password = '***'

options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
# options.add_argument("--headless")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options)

stealth(driver,
        user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36",
        languages=["uk-UA", "uk"],
        vendor="Google Inc.",
        platform="Win32",
        webgl_vendor="Intel Inc.",
        renderer="Intel Iris OpenGL Engine",
        fix_hairline=True,
        )

try:
    driver.get('https://www.instagram.com/')

    # Wait for the username input field to be clickable with an increased wait time
    username_input = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.NAME, 'username'))
    )

    username_input.clear()
    time.sleep(5)
    username_input.send_keys(username)

    # Wait for the password input field to be clickable
    password_input = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.NAME, 'password'))
    )

    password_input.clear()
    time.sleep(4)
    password_input.send_keys(password)
    time.sleep(1)
    password_input.send_keys(Keys.ENTER)

#     Add a wait for some element on the next page if needed
#     WebDriverWait(driver, 20).until(
#         EC.presence_of_element_located((By.XPATH, 'your_xpath_here'))
#     )

except NoSuchElementException as e:
    print(f"Element not found: {e}")

except Exception as e:
    print(f"An error occurred: {e}")

finally:
    # Ensure that the entire browser window is closed
    driver.quit()
解决方案
  • 修正浏览器指纹不匹配问题:当前代码中user_agent声明为Mac系统,但platform设置为Win32,这种矛盾的指纹会被Instagram的反爬虫系统识别。将platform改为"MacIntel",与user_agent保持一致。
  • 强化反检测配置:在ChromeOptions中添加options.add_argument("--disable-blink-features=AutomationControlled"),该参数可以进一步隐藏Selenium的自动化特征。
  • 使用真实浏览器指纹:不要混用不同设备的WebGL和渲染器信息,直接从你能正常登录的Chrome浏览器中提取真实的user_agent、languages、webgl_vendor等参数(可通过about:version页面查看)。
  • 替换固定等待为随机等待:把固定时长的time.sleep()改为随机区间等待,模拟更自然的用户操作节奏,避免机械行为被检测。
  • 复用本地浏览器配置:启动Chrome时加载你日常使用的用户配置文件,继承已有的登录环境和信任特征,示例代码如下:
    # Windows系统路径示例
    options.add_argument(r"user-data-dir=C:\Users\你的用户名\AppData\Local\Google\Chrome\User Data")
    # Mac系统路径示例
    # options.add_argument(r"user-data-dir=/Users/你的用户名/Library/Application Support/Google/Chrome/Default")
    
修复后的示例代码
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium_stealth import stealth
import time
import random

username = '***'
password = '***'

options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument("--disable-blink-features=AutomationControlled")  # 新增反检测参数
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
# 可选:复用本地配置文件
# options.add_argument(r"user-data-dir=C:\Users\你的用户名\AppData\Local\Google\Chrome\User Data")

driver = webdriver.Chrome(options=options)

stealth(driver,
        user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36",
        languages=["uk-UA", "uk"],
        vendor="Google Inc.",
        platform="MacIntel",  # 修正为与user_agent匹配的平台
        webgl_vendor="Intel Inc.",
        renderer="Intel Iris OpenGL Engine",
        fix_hairline=True,
        )

try:
    driver.get('https://www.instagram.com/')

    # 等待用户名输入框可点击
    username_input = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.NAME, 'username'))
    )
    username_input.clear()
    # 随机等待模拟用户停顿
    time.sleep(random.uniform(1, 3))
    username_input.send_keys(username)

    # 等待密码输入框可点击
    password_input = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.NAME, 'password'))
    )
    password_input.clear()
    time.sleep(random.uniform(1, 3))
    password_input.send_keys(password)
    time.sleep(random.uniform(0.5, 1.5))
    password_input.send_keys(Keys.ENTER)

    # 可选:等待登录后的页面元素
    # WebDriverWait(driver, 20).until(
    #     EC.presence_of_element_located((By.XPATH, "//div[contains(text(), '你的主页')]"))
    # )

except NoSuchElementException as e:
    print(f"元素未找到: {e}")

except Exception as e:
    print(f"发生错误: {e}")

finally:
    driver.quit()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 20:50:05