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

使用Python+Selenium查找Instagram动态上传的正确input标签

Python Selenium 实现Instagram动态图片上传的input定位方案

正确的input标签定位方式

Instagram移动端页面的动态上传input为type=file的隐藏元素,可通过以下属性定位,不会和头像上传的input混淆:

  • XPATH定位://input[@type='file' and contains(@accept, 'image/jpeg')]
  • CSS选择器定位:input[type="file"][accept*="image/jpeg"]

代码修改建议

你需要先确保驱动已正确配置移动端UA,否则PC端页面没有对应上传入口,修改后的参考代码如下:

def upload(self, image, text):
    # driver初始化时需要先配置移动端UA,示例UA:Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Safari/604.1
    driver.get("https://www.instagram.com/")
    time.sleep(3)
    # 定位动态上传专用input标签
    upload_input = driver.find_element(By.XPATH, "//input[@type='file' and contains(@accept, 'image/jpeg')]")
    # 发送本地图片路径,用raw字符串可避免反斜杠转义错误
    upload_input.send_keys(r"C:\Users\brend\PycharmProjects\ClimateCoinBot\PP2.jpg")
    # 后续可自行补充图片裁剪、添加配文、确认发布的逻辑
    time.sleep(2)

注意事项

  • 头像上传的input仅在个人资料编辑页加载,主页加载的type=file且accept包含多类图片/视频格式的元素就是动态上传专用input,不会出现定位错误
  • 不需要手动点击页面上的上传按钮,直接给隐藏的input元素传路径即可生效,不需要额外修改元素的可见性属性
  • 若需要上传视频,调整accept属性的匹配规则即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 13:00:01