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

使用Instapy 0.6.16登录Instagram遇NoSuchElementException问题求助

Instapy 0.6.16 登录Instagram失败(Login A/B test错误)

问题描述

使用Instapy 0.6.16版本登录Instagram,代码如下:

from instapy import Instapy
InstaPy(username="my_username", password="my_password").login()

连接检查和Cookie创建步骤正常,但出现以下错误:

INFO [2023-03-07 14:13:21] [my_username]  - Cookie file not found, creating cookie...
WARNING [2023-03-07 14:13:52] [my_username]  Login A/B test detected! Trying another string...
WARNING [2023-03-07 14:13:57] [my_username]  Could not pass the login A/B test. Trying last string...
ERROR [2023-03-07 14:14:02] [my_username]  Login A/B test failed!
        b"Message: Unable to locate element: //div[text()='Log In']\nStacktrace:\nRemoteError@chrome://remote/content/shared/RemoteError.sys.mjs:8:8\nWebDriverError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:180:5\nNoSuchElementError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:392:5\nelement.find/</<@chrome://remote/content/marionette/element.sys.mjs:133:16\n"

解决建议

  • 升级Instapy版本:0.6.16版本较旧,Instagram登录页面结构可能已更新,新版本通常会适配这些变化。执行命令升级:
    pip install --upgrade instapy
    
  • 手动登录生成Cookie:先手动打开浏览器登录Instagram,确保账号无异常(无验证弹窗),再让Instapy复用已有Cookie文件:
    from instapy import Instapy
    session = Instapy(username="my_username", password="my_password", cookie_path="./cookies/my_username_cookie.json")
    session.login()
    
  • 自定义登录按钮定位:错误提示找不到//div[text()='Log In']元素,说明登录按钮选择器已变更。手动确认当前按钮XPath后,用以下方式调整:
    from instapy import Instapy
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    session = Instapy(username="my_username", password="my_password")
    session.login()
    # 替换为实际可用的按钮XPath
    WebDriverWait(session.browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Log In')]"))).click()
    
  • 跳过A/B测试检测:尝试初始化时添加参数禁用A/B测试检测:
    session = Instapy(username="my_username", password="my_password", skip_login_ab_test=True)
    session.login()
    
  • 匹配浏览器驱动版本:确保ChromeDriver/GeckoDriver版本与本地浏览器版本一致,版本不兼容会导致元素定位失败。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 08:32:56