【WebDriver API】python之selenium上传文件

上传文件是比较常见的Web功能之一,但WebDriver并没有提供专门用于上传的方法,如何实现上传操作关键在于上传文件的思路。

一般Web页面上的上传功能的操作需要单击“上传”按钮后打开本地的Window窗口,从窗口选择本地文件进行上传。而WebDriver是无法操作Windows控件的,所以,对于初学者来说,一般思路会卡在如何识别Window控件这个问题上。

对于Web页面的上传功能实现一般有以下两种方式。

  • 普通上传:普通的附件上传是将本地文件的路径作为一个值放在input标签中,通过form表单将这个值提交给服务器。
  • 插件上传:一般是指基于Flash、JavaScript或Ajax等技术所实现的上传功能。

①send_keys实现上传

对于通过input标签实现了上传功能,可以将其看作是一个输入框,即通过send_keys()指定本地文件路径的方式实现文件上传。

upfile.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"></script>
</html>

===========================================================================================

通过浏览器打开upfile.html 文件,如下图

.

=============================================================================================

upfile.py

from selenium import webdriver
import os
import time
driver = webdriver.Chrome()
file_path='file:///'+os.path.abspath('upfile.html')
driver.get(file_path)

#定位上传按钮,添加本地文件
time.sleep(3)
driver.find_element_by_name("file").send_keys('D:\\upload_file.txt')
time.sleep(3)
driver.quit()

通过这种方法上传,就避免了操作Windows控件的步骤。如果能找到上传的input标签,那么基本上就可以通过send_keys()方法向其输入一个文件地址来实现上传。

②AutoIt实现上传

AutoIt目前最新版本是v3,它是一个使用类似BASIC脚本语言的免费软件,它被设计用来进行Windows GUI(图形用户界面)的自动化测试。它利用模拟键盘按键,鼠标移动和窗口/控件的组合来实现自动化任务。

官方网址:https://www.autoitscript.com/site/

1)点击AUTOIT--->DOWNLOADS

2)点击Download AutoIt

3)下载成功后,可以查看如下菜单。

AutoIt Window Info:用于识别Windows控件信息。

Compile Script to.exe:用于将AutoIt生成exe执行文件。

Run Script:用于执行AutoIt脚本。

SciTE Script Editor:用于编写AutoIt脚本。

下面以操作upload.html上传弹出的窗口为例,讲解AutoIt上传过程。

1.首先打开AutoIt Window Info工具,用鼠标点击Finder Tool,鼠标将变成一个小风扇形状的图标。按住鼠标左键,将其拖动到需要识别的控件上(如“打开”控制按钮)。

可以通过AutoIt Windows Info获得以下信息。

窗口的title为“选择要加载的文件”,标题的Class为“#32770”。

文件名输入框的class为“Edit”,Instance为“1”,所以ClassnameNN为“Edit1”。

打开按钮的class为“Button”,Instance为“1”,所以ClassnameNN为“Button1”。

2.根据AutoIt Windows Info所识别的控件信息,打开SCITE Script Edition编辑器,编写AutoIt的脚本。

upfile.au3

;ControlFocus("title","text",controlID) Edit1=Edit instance 1
ControlFocus("upload_file - Google Chrome","","Chrome_RenderWidgetHostHWND1")

;Wait 10 seconds for the Upload window to apper
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秒钟用于等待窗口的显示。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可执行文件。

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

4.接下来通过自动化测试脚本调用upfile.exe程序,实现上传。

upfile1.py

from selenium import webdriver
import os
import time
driver = webdriver.Chrome()
file_path='file:///'+os.path.abspath('upfile.html')
driver.get(file_path)

#定位上传按钮,添加本地文件
time.sleep(3)
driver.find_element_by_name("file").click()
#调用upfile.exe上传程序
os.system("D:\\upfile.exe")
time.sleep(3)
driver.quit()

通过system()方法可以调用并执行upfile.exe程序。

虽然这种方式可以解决文件上传(或文件下载)的操作问题,但不太推荐这种解决方案,因为通过Python调用的exe程序并不是Python的可控范围内。换句话说,exe执行多长时间,执行是否出错,Python程序都无法得值。

发布了30 篇原创文章 · 获赞 7 · 访问量 6785

猜你喜欢

转载自blog.csdn.net/w68688686/article/details/104039533
今日推荐