当前位置:网站首页>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 .
边栏推荐
- How to design an elegant caching function
- Special tutorial - Captain selection game
- Go from introduction to practice - polymorphism (note)
- Remote invocation of microservices
- 熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊
- How to delete "know this picture" on win11 desktop
- xpath
- GBase 8a OLAP分析函数 cume_dist的使用样例
- Gbase 8A method for reducing the impact on the system by controlling resource usage through concurrency during node replacement of V8 version
- 管理系統-ITclub(下)
猜你喜欢
![[LeetCode]动态规划解分割数组I[Red Fox]](/img/b2/df87c3138c28e83a8a58f80b2938b8.png)
[LeetCode]动态规划解分割数组I[Red Fox]

Système de gestion - itclub (II)

Go from introduction to practice - Interface (notes)

Knowledge sorting of exception handling

Go从入门到实战——任务的取消(笔记)

【MySQL】数据库函数通关教程下篇(窗口函数专题)

AQS SOS AQS with me
![\w和[A-Za-z0-9_],\d和[0-9]等价吗?](/img/96/2649c9cf95b06887b57fd8af2d41c2.png)
\w和[A-Za-z0-9_],\d和[0-9]等价吗?

Go from introduction to practice - error mechanism (note)

At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions
随机推荐
Method of reading file contents by Excel
【MySQL】数据库函数通关教程下篇(窗口函数专题)
Go from starting to Real - Interface (note)
大厂常用软件测试面试题三(附答案)
[leetcode] dynamic programming solution partition array i[red fox]
Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
[LeetCode]508. 出现次数最多的子树元素和
软件缺陷管理——测试人员必会
Selenium上传文件有多少种方式?不信你有我全!
Matlab finds the position of a row or column in the matrix
Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)
Quick excel export according to customized excel Title Template
\W and [a-za-z0-9_], \Are D and [0-9] equivalent?
Go from introduction to practice -- definition and implementation of behavior (notes)
[leetcode] dynamic programming solution partition array ii[arctic fox]
Read write separation master-slave replication of MySQL
Summary of Web testing and app testing by bat testing experts
"Apprendre cette image" apparaît sur le Bureau win11 comment supprimer
Go 访问GBase 8a 数据库的一个方法
Special tutorial - Captain selection game