当前位置:网站首页>How many ways does selenium upload files? I don't believe you have me all!
How many ways does selenium upload files? I don't believe you have me all!
2022-06-27 22:00:00 【Software testing】
Selenium Encapsulates the ready-made file upload operation . But with the development of modern front-end framework , There are more and more ways to upload files . And there are some file upload controls , It will be more complicated to do automatic control , This article mainly discusses in complex situations , How to upload files automatically
1、input Element upload file
If the page needs file upload , So in most cases , Can be found in the page source code input The elements of .
If you can see this directly in the page input Elements , Then through the selenium Of send_keys Method can complete the file upload , Pass in the path of the local file in the parameter .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 Hidden elements
By modifying element attributes , Change the hidden element attributes .
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')
For example, Baidu's search for pictures can be realized in this way .
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. File selection dialog
For some elements , Directly through selenium Self contained send_keys Method to upload the file will not succeed . If you don't want to be right input Too much analysis of elements , Then the more direct way is to use the file upload dialog box to deal with .
Generally speaking , If you need to upload files , So when you click on this element , A file upload dialog box will appear , Ask you to choose a file , And click OK . This dialog box belongs to the system , therefore selenium You can't control it directly . We can use the automation tools of the system or directly call the keyboard to operate this dialog box .
Before operating the dialog , First we pass selenium Click the file upload element .
el = driver.find_element('id', "fileinput")
ActionChains(driver).click(el).perform()
input You can't click on the element , So you can't use element el.click() Method , Need to use ActionChains Below click Method . The difference between them is the of elements el.click The method is more strict , Whether the element is visible , Whether you can click to detect , After the click event takes full effect , Do the following again , If these conditions are not met , There may be a mistake . and Action Under the click The method is much rougher , It hardly detects elements , Move the mouse directly over the element , Perform the click operation , As for whether the click takes effect , It doesn't matter at all .
4. Use pywinauto Upload files
pywinauto yes Windows An automation tool under the system , It can directly obtain Windows The bullet box under the system , So when the file upload window appears , We can use this tool to pass in the path of the file , Then click the open button .
from pywinauto import Desktop
app = Desktop()
dialog = app[' open '] # Find the pop-up window by name
dialog["Edit"].type_keys('/path/of/file.md') # Enter the value... In the input box
dialog["Button"].click()
Another system automation tool is called pyautogui. The biggest feature of this tool is that it uses the coordinate system to locate elements , You can easily cross platform . No matter you are Windows,mac still Linux, You can use this tool to automate .
However, this tool does not support Chinese input at present , Therefore, we need to use the shear board to realize the input . First, we copy the corresponding Chinese into the clipboard , And then through ctrl + v Paste the hotkey into the file path input box .
5. pyautogui
import pyperclip
pyperclip.copy('D:\\\\ user .html')
pyautogui.hotkey('ctrl', 'v')
pyautogui.press('enter', presses=2)
keyboard
keyboard.write('C:\\\\Users\\\\muji\\\\Desktop\\\\avatar.png')
time.sleep(1)
keyboard.press('enter')
Be careful : Baidu has disabled crawlers by searching for pictures , So when you upload a file, you will be prompted 「 Picture upload failed , Please upload again 」.
6. Concurrency issues
Uploading files through the system window is simple and crude , But when your program needs to be executed concurrently , Using this method to upload files is more troublesome . If your program needs to be executed concurrently , It's better to control input Elements , Use send_keys Method to upload files .
The house needs to be built layer by layer , Knowledge needs to be learned at one point one . We should lay a good foundation in the process of learning , More hands-on practice , Don't talk much , The last dry goods here ! I stayed up late to sort out the stages ( function 、 Interface 、 automation 、 performance 、 Test open ) Skills learning materials + Practical explanation , Very suitable for studying in private , It's much more efficient than self-study , Share with you .
Get off w/x/g/z/h: Software testing tips dao
Typing is not easy , If this article is helpful to you , Click a like, collect a hide and pay attention , Give the author an encouragement . It's also convenient for you to find it quickly next time .
边栏推荐
- GBase 8a OLAP函数group by grouping sets的使用样例
- regular expression
- Installing Oracle11g under Linux
- "Apprendre cette image" apparaît sur le Bureau win11 comment supprimer
- 语言弱点列表--CWE,一个值得学习的网站
- 软件缺陷管理——测试人员必会
- Array assignment
- [leetcode] 508. Élément de sous - arbre le plus fréquent et
- Go from introduction to practice -- shared memory concurrency mechanism (notes)
- Interval DP of Changyou dynamic programming
猜你喜欢

不外泄的测试用例设计秘籍--模块测试

单元测试界的高富帅,Pytest框架,手把手教学,以后测试报告就这么做~

The create database of gbase 8A takes a long time to query and is suspected to be stuck
![\W and [a-za-z0-9_], \Are D and [0-9] equivalent?](/img/96/2649c9cf95b06887b57fd8af2d41c2.png)
\W and [a-za-z0-9_], \Are D and [0-9] equivalent?

Knowledge sorting of exception handling

Bit.Store:熊市漫漫,稳定Staking产品或成主旋律

Go from introduction to practice -- coordination mechanism (note)

Special training of guessing game

Open source technology exchange - Introduction to Chengying, a one-stop fully automated operation and maintenance manager

Go from introduction to practice -- shared memory concurrency mechanism (notes)
随机推荐
Simulink导出FMU模型文件方法
The create database of gbase 8A takes a long time to query and is suspected to be stuck
Go from starting to Real - Interface (note)
Null pointer exception
开源技术交流丨一站式全自动化运维管家ChengYing入门介绍
[LeetCode]161. 相隔为 1 的编辑距离
Summary of gbase 8A database user password security related parameters
Go from introduction to practice - error mechanism (note)
Interval DP of Changyou dynamic programming
Go from introduction to practice - Interface (notes)
win11桌面出現“了解此圖片”如何删除
[LeetCode]572. 另一棵树的子树
图解基于AQS队列实现的CountDownLatch和CyclicBarrier
大厂常用软件测试面试题三(附答案)
Gbase 8A method for reducing the impact on the system by controlling resource usage through concurrency during node replacement of V8 version
石子合并问题分析
Go从入门到实战——仅执行一次(笔记)
QT large file generation MD5 check code
Management system itclub (Part 2)
Xiao Wang's interview training task