当前位置:网站首页>Selenium uploads files in more ways than you think
Selenium uploads files in more ways than you think
2022-07-24 20:23:00 【Zeze said test】
Thank you for your reading and company , I put the automatic test content that I usually use more into a small volume of the system , You can directly click the text below to read , I hope to provide a little help for those who are destined .
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 .
<input type="file" name="file_name">
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 .
I'm nine , Thank you for your patience in reading , See you next time .
边栏推荐
- VLAN Technology
- [training Day6] dream [priority queue] [greed]
- Richview table table alignment
- YouTube "label products" pilot project launched
- When using vscode, the tab indentation changes from 4 spaces to small arrows (solved)
- 02 | environment preparation: how to install and configure a basic PHP development environment under windows?
- Functional test of redisgraph multi active design scheme
- [training Day10] linear [mathematics] [thinking]
- Valdo2021 - vascular space segmentation in vascular disease detection challenge (3)
- Each blogger needs to ask himself seven basic questions
猜你喜欢

API data interface for historical data of A-share index

Istio一之Envoy工作原理

Working principle of envy of istio I

Actual measurement of Qunhui 71000 Gigabit Network

Make Huawei router into FTP server (realize upload and download function)

What should Ali pay attention to during the interview? Personal account of Alibaba interns who passed five rounds of interviews

TCP sliding window, singleton mode (lazy and hungry) double checked locking / double checked locking (DCL)

From code farmer to great musician, you only need these music processing tools

Thinking of @requestbody caused by hi and hello requests
![[training Day10] point [enumeration] [bidirectional linked list]](/img/62/41dcab40eeb6aea545602e10c1c1a0.png)
[training Day10] point [enumeration] [bidirectional linked list]
随机推荐
Browser local storage webstroage
YouTube "label products" pilot project launched
[training Day9] rotate [violence] [thinking]
[FreeRTOS] 10 event flag group
Processing of null value of Oracle notes
Introduction to WDK development 1- basic environment construction and the first driver (VS2010)
[training Day10] linear [mathematics] [thinking]
Istio一之Envoy工作原理
Thinking of @requestbody caused by hi and hello requests
Hook 32-bit function using the method modified to JMP instruction
Redis common configuration description
Applet wonderful bug update~
SSL Error: Unable to verify the first certificate
[German flavor] safety: how to provide more protection for pedestrians
Modulenotfounderror: no module named 'pysat.solvers' (resolved)
Leetcode 1928. minimum cost of reaching the destination within the specified time
1. Mx6u-alpha development board (buzzer experiment)
英文翻译中文常见脏话
Stop using UUID indiscriminately. Have you tested the performance gap between self incrementing ID and UUID?
Solve the problem of error l6218e undefined symbol XXX