selenium和AutoIt工具辅助下载和上传

上传

根据AutoIt Windows Info 所识别到的控件信息打开SciTE Script Editor编辑器,编写脚本。

复制代码
;ControlFocus("title","text",controlID) Edit1=Edit instance 1
ControlFocus("选择要加载的文件", "","Edit1")


; Wait 10 seconds for the Upload window to appear
  WinWait("[CLASS:#32770]","",10)


; Set the File name text on the Edit field
//要上传的文件
  ControlSetText("选择要加载的文件", "", "Edit1", "D:\upload_file.txt")

  Sleep(2000)

; Click on the Open button

  ControlClick("选择要加载的文件", "","Button1");
复制代码

   ControlFocus()方法用于识别Window窗口。WinWait()设置10秒钟用于等待窗口的显示,其用法与WebDriver 所提供的implicitly_wait()类似。ControlSetText()用于向“文件名”输入框内输入本地文件的路径。这里的Sleep()方法与Python中time模块提供的Sleep()方法用法一样,不过它是以毫秒为单位,Sleep(2000)表示固定休眠2000毫秒。ControlClick()用于点击上传窗口中的“打开”按钮。

  AutoIt的脚本已经写好了,可以通过菜单栏“Tools”-->“Go” (或按键盘F5)来运行一个脚本吧!注意在运行时上传窗口当前处于打开状态。

 

  3、脚本运行正常,将其保存为upfile.au3,这里保存的脚本可以通过Run Script 工具将其打开运行,但我们的目的是希望这个脚本被Python程序调用,那么就需要将其生成exe程序。打开Compile Script to.exe工具,将其生成为exe可执行文件。如图4.16,

                              图4.16  Compile Script to.exe生成exe程序

点击“Browse”选择upfile.au3文件,点击“Convert”按钮将其生成为upfile.exe程序。

 

4、下面就是通过自动化测试脚本调用upfile.exe程序实现上传了。

复制代码
#coding=utf-8
from selenium import webdriver
import os

driver = webdriver.Firefox()

#打开上传功能页面
file_path =  'file:///' + os.path.abspath('upfile.html')
driver.get(file_path)

#点击打开上传窗口
driver.find_element_by_name("file").click()
#调用upfile.exe上传程序
os.system("D:\upfile.exe")

driver.quit()
复制代码
 
 
下载  用工具盒编译成exe
ControlFocus("另存为", "","Edit1");ControlFocus("title","text",controlID) Edit1=Edit instance 1
; Wait 10 seconds for the Upload window to appear

  WinWait("[CLASS:#32770]","",10)

; Set input focus to the edit control of Upload window using the handle returned by WinWait

  ControlFocus("另存为","","Edit1")

  Sleep(2000)

; Set the File name text on the Edit field
//下载的保存路径
  ControlSetText("另存为", "", "Edit1", "d:autoit-v3-setup")

  Sleep(2000)

; Click on the Open button

  ControlClick("另存为", "","Button1");

ControlFocus("另存为", "","Edit1");ControlFocus("title","text",controlID) Edit1=Edit instance 1
; Wait 10 seconds for the Upload window to appear

  WinWait("[CLASS:#32770]","",10)

; Set input focus to the edit control of Upload window using the handle returned by WinWait

  ControlFocus("另存为","","Edit1")

  Sleep(2000)

; Set the File name text on the Edit field

  ControlSetText("另存为", "", "Edit1", "d:autoit-v3-setup")

  Sleep(2000)

; Click on the Open button

  ControlClick("另存为", "","Button1");

原文地址:https://www.cnblogs.com/gyadmin/p/10449716.html