SoFunction
Updated on 2024-10-29

Several ways to upload files in Selenium in Python

Selenium encapsulates out-of-the-box file upload operations. However, with the development of modern front-end frameworks, there are more and more different ways to upload files. There are some file upload controls that are a bit more complicated to automate, and this article focuses on how to automate file uploads in complex situations.

1. input element to upload a file

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

<input type="file" name="file_name">

If you can see the input element directly on the page, you can upload the file via selenium's send_keys method, passing the path to the local file in the parameters.

('</styled/>')

el = driver.find_element('id', "fileinput")
el.send_keys('/path/of/')

2. input element hidden

Change the hidden element attributes by modifying them.

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

For example, you can realize Baidu's map search in this way.

('<>')
driver.find_element('css selector', '.soutu-btn').click()
(3)
el = driver.find_element('xpath', '//input[@type="file"]')
driver.execute_script('arguments[0].=\\'visible\\'', el)
el.send_keys(r'C:\\Users\\muji\\Desktop\\')

3. Document selection dialog box

For some elements, uploading a file directly through selenium's own send_keys method won't work. If you don't want to analyze the input element too much, it's more straightforward to use the file upload dialog to handle it.

In general, if you need to upload a file, then after you click on this element, a file upload dialog will appear asking you to select the file and click OK. This dialog belongs to the system, so selenium has no direct control over it. We can use the system's automation tools or call the keyboard directly to manipulate this dialog.

Before manipulating the dialog, first we click on the file upload element via 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 () method, you need to use the click method under ActionChains. The difference between them is that the element's method is much stricter, it will test whether the element is visible and clickable, and then perform the following actions after the click event has taken full effect, if these conditions are not met, it may report an error. The click method under Action is much cruder, it hardly tests the element, it just moves the mouse over the element and performs the click, it doesn't care whether the click is valid or not.

4. Uploading files with pywinauto

pywinauto is an automated tool for Windows that can directly access the Windows pop-up box, so when the file upload window appears, we can use this tool to pass in the path to the file and then click the Open button.

from pywinauto import Desktop

app = Desktop()
dialog = app['Open']  # Find pop-ups by name
dialog["Edit"].type_keys('/path/of/')  # Enter a value in the input box
dialog["Button"].click()

Another system automation tool is called pyautogui, which is unique in that it uses a coordinate system to locate elements and is easily cross-platform. It doesn't matter if you have Windows, mac or Linux, you can automate with this tool.

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

5. pyautogui

 import pyperclip

 ('D:\\\\ users.html')
 ('ctrl', 'v')
 ('enter', presses=2)

keypads

('C:\\\\Users\\\\muji\\\\Desktop\\\\')
(1)
('enter')

Note: Baidu has disabled the crawler, so you will be prompted "Image upload failed, please re-upload" when uploading files.

6. Concurrency issues

Uploading a file through the system window is simple and brute force, but when your program needs to be executed concurrently, it's a bit tricky to achieve file uploads in this way. If your program requires concurrent execution, it is best to control the input element and use the send_keys method to upload files.

This article on Python Selenium upload files in several ways to this article, more related to Python Selenium upload file content please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!