当前位置:网站首页>Selenium上传文件有多少种方式?不信你有我全!
Selenium上传文件有多少种方式?不信你有我全!
2022-06-27 19:36:00 【软件测试呀】
Selenium 封装了现成的文件上传操作。但是随着现代前端框架的发展,文件上传的方式越来越多样。而有一些文件上传的控件,要做自动化控制会更复杂一些,这篇文章主要讨论在复杂情况下,如何通过自动化完成文件上传
1、input 元素上传文件
如果页面需要文件上传,那么在大多数情况下,都能在页面源代码中找到一个 input 的元素。
如果能直接在页面当中看到这个 input 元素,那么通过 selenium 的 send_keys 方法就能完成文件的上传,在参数中传入本地文件的路径。driver.get('<https://testpages.herokuapp.com/styled/file-upload-test.html>')
el = driver.find_element('id', "fileinput")
el.send_keys('/path/of/file.png')
2.input 元素隐藏
通过修改元素属性,把隐藏的元素属性改掉。
el = driver.find_element('xpath', '//input[@type="file"]')
driver.execute_script('arguments[0].style.visibility=\\'visible\\'', el)
el.send_keys(r'C:\\Users\\muji\\Desktop\\avatar.png')
比如可以通过这种方式实现百度的以图搜图。
driver.get('<http://www.baidu.com>')
driver.find_element('css selector', '.soutu-btn').click()
time.sleep(3)
el = driver.find_element('xpath', '//input[@type="file"]')
driver.execute_script('arguments[0].style.visibility=\\'visible\\'', el)
el.send_keys(r'C:\\Users\\muji\\Desktop\\avatar.png')
3. 文件选择对话框
对于有一些元素,直接通过 selenium 自带的 send_keys 方法上传文件并不会成功。如果不想对 input 元素进行过多的分析,那么比较直接的方式是采用文件上传对话框来处理。
一般来说,如果需要上传文件,那么当你点击这个元素之后,会出现一个文件上传的对话框,要求你选择文件,并点击确定。这个对话框是属于系统的,因此 selenium 无法直接控制它。我们可以采用系统的自动化工具或者直接调用键盘来操作这种对话框。
在操作对话框之前,首先我们通过 selenium 点击文件上传的元素。
el = driver.find_element('id', "fileinput")
ActionChains(driver).click(el).perform()
input 元素并不能点击,所以不能用元素的 el.click() 方法,需要使用 ActionChains 下面的 click 方法。他们之间的区别在于元素的 el.click 方法更加严格,会对元素是否可见,是否可点击进行检测,在点击事件完全生效后,再执行下面的操作,如果不满足这些条件,可能会报错。而 Action 下的 click 方法则粗暴得多,它几乎不对对元素进行检测,直接把鼠标移动到元素上方,执行点击操作,至于点击是否生效,根本不管。
4. 使用 pywinauto 上传文件
pywinauto 是 Windows 系统下的一个自动化工具,它可以直接获取到 Windows 系统下面的弹框,因此当文件上传的窗口出现之后,我们可以使用这个工具传入文件的路径,然后点击打开按钮。
from pywinauto import Desktop
app = Desktop()
dialog = app['打开'] # 根据名字找到弹出窗口
dialog["Edit"].type_keys('/path/of/file.md') # 在输入框中输入值
dialog["Button"].click()
另一种系统自动化工具叫做 pyautogui。这个工具最大的特点在于使用坐标系统定位元素,可以轻而易举的做到跨平台。不管你是 Windows,mac 还是 Linux,都可以使用这款工具实现自动化。
但是这款工具目前不支持中文的输入,因此我们需要借助剪切板来实现中输入。首先我们把对应的中文复制到剪切板当中,然后再通过 ctrl + v 热键粘贴到文件路径输入框当中。
5. pyautogui
import pyperclip
pyperclip.copy('D:\\\\用户.html')
pyautogui.hotkey('ctrl', 'v')
pyautogui.press('enter', presses=2)
键盘
keyboard.write('C:\\\\Users\\\\muji\\\\Desktop\\\\avatar.png')
time.sleep(1)
keyboard.press('enter')
注意:百度以图搜图禁用了爬虫,所以在上传文件时会提示「图片上传失败,请重新上传」。
6.并发问题
通过系统窗口上传文件简单粗暴,但是当你的程序需要并发执行时,使用这种方式实现文件上传就比较麻烦了。如果你的程序需要并发执行,最好还是通过控制 input 元素,使用 send_keys 方法实现文件上传。
房子要一层一层盖,知识要一点一点学。大家在学习过程中要好基础,多上手实操,话不多说,这里狠狠上一次干货!我熬夜整理好的各阶段(功能、接口、自动化、性能、测开)技能学习资料+实操讲解,非常适合私下里学习,比找资料自学高效多了,分享给你们。
领取关 w/x/g/z/h:软件测试小dao
敲字不易,如果此文章对你有帮助的话,点个赞收个藏来个关注,给作者一个鼓励。也方便你下次能够快速查找。
边栏推荐
- Acwing周赛57-最长连续子序列-(二分or树状数组)
- 豆沙绿保护你的双眼
- .NET学习笔记(五)----Lambda、Linq、匿名类(var)、扩展方法
- 我想我要开始写我自己的博客了。
- Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
- 神奇的POI读取excel模板文件报错
- VMware virtual machine PE startup
- IO stream code
- AQS SOS AQS with me
- Tiktok's interest in e-commerce has hit the traffic ceiling?
猜你喜欢

vmware虚拟机PE启动

∫(0→1) ln(1+x) / (x² + 1) dx

Simulink method for exporting FMU model files

【Redis】零基础十分钟学会Redis

Let Ma Huateng down! Web3.0, hopeless

Go从入门到实战——Panic和recover(笔记)

After being forced to develop the app within 20 days, the group was laid off, and the technical director angrily criticized it: I wish "closure as soon as possible!"
![[leetcode] dynamic programming solution split integer i[silver fox]](/img/18/8dc8159037ec1262444db8899cde0c.png)
[leetcode] dynamic programming solution split integer i[silver fox]

What is the core competitiveness of front-line R & D personnel aged 35~40 in this position?

AQS SOS AQS with me
随机推荐
Common methods of string class
关于异常处理的知识整理
Acwing周赛57-最长连续子序列-(二分or树状数组)
[LeetCode]30. Concatenate substrings of all words
Set code exercise
Special tutorial - Captain selection game
Go从入门到实战—— 多路选择和超时控制(笔记)
微服务之远程调用
∫(0→1) ln(1+x) / (x ² + 1) dx
[LeetCode]186. Flip word II in string
我想我要开始写我自己的博客了。
Go从入门到实战——协程机制(笔记)
熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊
Xiao Wang's interview training task
String类的常用方法
ABC-Teleporter Setting-(思维+最短路)
Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
根据自定义excel标题模板快速excel导出
What is the core competitiveness of front-line R & D personnel aged 35~40 in this position?
Express e stack - small items in array