Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Selenium browser Automation how to upload Files

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "Selenium browser automation how to upload files". In daily operation, I believe many people have doubts about how to upload files in Selenium browser automation. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "Selenium browser automation how to upload files". Next, please follow the editor to study!

Selenium encapsulates ready-made file upload operations. However, with the development of modern front-end framework, there are more and more ways to upload files.

1. Upload files with input element

If the page requires file upload, in most cases, an element of input can be found in the source code of the page.

If you can see the input element directly in the page, you can upload the file through the send_keys method of selenium, passing in the path of the local file in the parameters.

Driver.get ('') el = driver.find_element ('id', "fileinput") el.send_keys (' / path/of/file.png') 2. Input element hidden

Change the hidden element attributes by modifying the element attributes.

El = driver.find_element ('xpath',' / / input [@ type= "file]') driver.execute_script ('arguments [0] .style.style =\\' visible\', el) el.send_keys (ritual C:\\ Users\\ muji\\ Desktop\\ avatar.png')

For example, Baidu's map search can be realized in this way.

Driver.get ('') 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.style =\\' visible\', el) el.send_keys (\\ Users\\ muji\\ Desktop\\ avatar.png') 3. File selection dialog box

For some elements, uploading files directly through the send_keys method that comes with selenium will not be successful. If you don't want to do too much analysis of the input element, then the more direct way is to use the file upload dialog box to deal with it.

In general, if you need to upload a file, then when you click on this element, a file upload dialog box will appear, asking you to select the file and click OK. This dialog belongs to the system, so selenium cannot directly control it. We can use the automation tools of the system or directly call the keyboard to operate this dialog box.

Before operating the dialog box, first click on the element of the file upload through selenium.

El = driver.find_element ('id', "fileinput") ActionChains (driver) .click (el) .perform ()

The input element is not clickable, so you can't use the element's el.click () method, you need to use the click method under ActionChains. The difference between them is that the el.click method of the element is more stringent, which will check whether the element is visible and clickable, and then perform the following actions after the click event is fully effective. If these conditions are not met, an error may be reported. The click method under Action is much rougher, it hardly detects the element, moves the mouse over the element directly, and performs a click, regardless of whether the click is effective or not.

4. Upload files using pywinauto

Pywinauto is an automated tool under the Windows system, which can directly get the pop-up box under the Windows system, so when the file upload window appears, we can use this tool to pass in the path of the file and click the open button.

From pywinauto import Desktopapp = Desktop () dialog = app ['Open'] # find the pop-up window dialog ["Edit"] .type_keys ('/ path/of/file.md') # enter the value dialog ["Button"] .click () in the input box

Another system automation tool is called pyautogui. The biggest feature of this tool is the use of coordinate systems to locate elements, which can be easily achieved across platforms. Whether you are Windows,mac or Linux, you can use this tool to automate.

However, this tool does not support Chinese input at present, so we need to use the clipboard to achieve Chinese input. First, we copy the corresponding Chinese to the clipboard, and then paste it into the file path input box through the ctrl + v hotkey.

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')

Note: Baidu disabled crawlers in image search, so it will prompt "picture upload failed, please upload again" when uploading files.

6. Concurrency problem

Uploading files through the system window is simple and rude, but when your program needs to be executed concurrently, it is more troublesome to upload files in this way. If your program needs to be executed concurrently, it is best to use the send_keys method to upload files by controlling the input element.

At this point, the study on "how to upload files automatically in Selenium browsers" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report