一、下载软件AutoIt3安装:
链接:https://pan.baidu.com/s/1Ed13tC-agytq_q4lff_w7A 提取码:8x7x
中文文档:https://www.autoitx.com/Doc/
安装步骤就不介绍了,本文以百度上传附件为例:
二、点击“选择文件”,弹出上传附件窗口:
三、进入安装AutoIt3目录,找到Au3Info_x64.exe文件打开:
四、鼠标点击finder tool拖动到文件输入框:
五、再次拖动Finder Tool拖动到打开按键上:
六、进入AutoIt3安装文件夹找到SciTE.exe打开:
输入下面信息:
;ControlFocus("title","text",controlID) Edit1=Edit instance 1
ControlFocus("文件上传", "","Edit")
;Wait 10 seconds for the Upload window to appear
WinWait("[CLASS:#32770]","",10)
Sleep(2000)
;Get random pictures
$d = Random(1,200,1)
$s = StringFormat("D:\py\pic\baidu\%d.jpg",$d)
;Set the File name text on the Edit field
ControlSetText("文件上传", "", "Edit1", $s)
Sleep(2000)
;Click on the Open button
;ControlClick ( "标题", "文本", 控件ID [, 按钮 [, 点击次数 [, X坐标 [, Y坐标]]]] )
; ControlClick("文件上传", "","Button");
ControlClick("文件上传", "","Button1","left",1)
;click enter 如无法点击弹出窗口{打开}按钮时,加上此代码
; Send("{ENTER}")
七、点击Tools–compile后会生成一个输入文件,记住地址
八、编写python脚本:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
import time
driver = webdriver.Firefox()
driver.maximize_window()
driver.implicitly_wait(10)
driver.get('https://www.baidu.com')
driver.find_element_by_css_selector('span.soutu-btn').click()
#driver.find_element_by_css_selector('.upload-pic').click()
#driver.find_element_by_xpath('//input[@class="upload-pic"]').click()
#driver.find_element_by_css_selector('input.upload-pic').send_keys(Keys.ENTER)
#driver.find_element_by_css_selector('span.upload-text-new').click()
driver.find_element_by_css_selector('div.upload-wrap').click() #试了几种办法才定位到“选择文件”按钮,定位到按钮父级
os.system(r'D:\fire.exe') #地址为刚刚AutoIt3输入的地址