文件上传

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>upload_file</title>
<link href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
  <div class="row-fluid">
    <div class="span6 well">
    <h3>upload_file</h3>
      <input type="file" name="file" />
    </div>
  </div>
</body>
<script src="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.js"></scrip>
</html>
from selenium import webdriver
import os
from time import sleep
import win32gui
import win32con

driver = webdriver.Chrome()
file_path = 'file:///' + os.path.abspath('upfile.html')
# print(file_path)
driver.get(file_path)

# 定位上传按钮,添加本地文件
# 不使用同目录下的text.txt文档,是因为G盘的text.txt文档在选择文件时的windows窗口太小了,查找不到G盘无法点击
driver.find_element_by_name("file").send_keys(r'C:UserschenyuDesktop1.txt')
sleep(2)
driver.quit()
原文地址:https://www.cnblogs.com/cy-zjs/p/13999986.html