5、编写AutoIt脚本,实现文件上传
1.打开scite script editor
代码如下:
# coding=utf-8
import time
from selenium import webdriver
driver = webdriver.Firefox() #打开火狐浏览器
driver.get('http://www.baidu.com') #打开百度界面
driver.find_element_by_id('kw').send_keys('China') #在搜索框内输入想要搜索内容
time.sleep(2) # 浏览器加载需要时间
driver.find_element_by_id('su').click() #搜索完成
这块的代码编写完成后,先测试下。
tools---》Go 运行无误后,然后把au3文件转换成exe。
4. 使用compile script to exe将上述AutoIt脚本编译为exe文件供python脚本调用
点击ok即可。
6、最后,使用Python脚本调用AutoIT脚本
完整代码如下:
import os
from selenium import webdriver
import time
#class Connect():
UserName = ''
PassWord = ''
URL = 'https://pan.baidu.com/'
driver = webdriver.Firefox()
driver.get(URL)
driver.find_element_by_xpath('/html/body/div[1]/div[3]/div[6]/div/div[6]/div[2]/a').click()
driver.find_element_by_xpath('/html/body/div[1]/div[3]/div[6]/div/div[3]/form/p[5]/input[2]').send_keys(UserName )
driver.find_element_by_xpath('/html/body/div[1]/div[3]/div[6]/div/div[3]/form/p[6]/input[2]').send_keys(PassWord )
driver.find_element_by_xpath('/html/body/div[1]/div[3]/div[6]/div/div[3]/form/p[9]/input').click()
time.sleep(30) #设置按钮响应的时间
sreach_window = driver.current_window_handle #此行代码用来定位当前页面
driver.find_element_by_xpath('//*[@id="h5Input0"]').click()
os.system(r'C:\\Users\xx\Desktop\upfile.exe')
我的运行结果如下: