当前位置:网站首页>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的使用样例
- [leetcode] dynamic programming solution partition array i[red fox]
- Special training of guessing game
- 开源技术交流丨一站式全自动化运维管家ChengYing入门介绍
- Little known MySQL import data
- Go从入门到实战——仅执行一次(笔记)
- Go from introduction to practice - polymorphism (note)
- ∫(0→1) ln(1+x) / (x² + 1) dx
- GBase 8a OLAP分析函数 cume_dist的使用样例
- Luogu p5706 redistributing fertilizer and house water
猜你喜欢

洛谷P5706 再分肥宅水

Figure countdownlatch and cyclicbarrier based on AQS queue
![[MySQL] database function clearance Tutorial Part 2 (window function topic)](/img/03/2b37e63d0d482d5020b7421ac974cb.jpg)
[MySQL] database function clearance Tutorial Part 2 (window function topic)

Go from introduction to practice - error mechanism (note)

I think I should start writing my own blog.

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

Read write separation master-slave replication of MySQL

The create database of gbase 8A takes a long time to query and is suspected to be stuck

VMware virtual machine PE startup

Go from introduction to practice - Interface (notes)
随机推荐
qt 大文件生成md5校验码
[LeetCode]161. 相隔为 1 的编辑距离
C language programming detailed version (learning note 1) I can't understand it after reading, and I can't help it.
VMware virtual machine PE startup
C语言程序设计详细版 (学习笔记1) 看完不懂,我也没办法。
Stm32f107+lan8720a use stm32subemx to configure network connection +tcp master-slave +udp app
win11桌面出現“了解此圖片”如何删除
开源技术交流丨一站式全自动化运维管家ChengYing入门介绍
[LeetCode]515. 在每个树行中找最大值
[LeetCode]508. 出現次數最多的子樹元素和
GBase 8a OLAP分析函数 cume_dist的使用样例
Figure countdownlatch and cyclicbarrier based on AQS queue
[leetcode] dynamic programming solution partition array ii[arctic fox]
快速excel导出
软件测试自动化测试之——接口测试从入门到精通,每天学习一点点
Go from introduction to practice - Interface (notes)
[Sword Offer II]剑指 Offer II 029. 排序的循环链表
[LeetCode]572. 另一棵树的子树
[LeetCode]508. The most frequent subtree elements and
How to design an elegant caching function