selenium 文件上传

一、<input type=file>通过send_keys()发送文件路径

  

   

 二、借助pywinauto第三方库上传

  通过其他一些方式实现的上传功能(非<input type='file'>),或者说想传多个文件的时候,就必须要采用按钮点击的方式,这种方当点击上传按钮后,会弹出文件选择框,这个选择框是脱离浏览器的,因此无法直接使用selenium来操作。

  下载pywinauto插件,方法一:terminal终端pip install pywinauto,方法二:File >> Settings >> Project Interpreter >> 右侧+ >> 搜索pywinauto >> Install Package(不要勾选Install to user's site packages directory)

  方式一:send_keys(文件路径),send_keys('{VK_RETURN}')(如果这个方式有问题,使用方式二)

  

   方式二:使用Desktop()类

  

原文地址:https://www.cnblogs.com/purelavender/p/14546896.html