Make writing a habit together! This is the fourth day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

Selenium encapsulates off-the-shelf file upload operations. But with the development of modern front-end frameworks, there are more and more ways to upload files. And some file upload control, to do automatic control will be more complex, this article mainly discusses in complex circumstances, how to complete the file upload through automation.

1. Upload the input element

If a page requires file uploads, in most cases you’ll find an input element in the page source code.

<input type="file" name="file_name">
Copy the code

If you can see the input element directly on the page, you can upload the file using selenium’s send_keys method, passing in the path to the local file as a 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')
Copy the code

2. Input elements are hidden

Change hidden element attributes by modifying 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')
Copy the code

For example, baidu can be achieved in this way to search for maps.

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')
Copy the code

3. File selection dialog box

For some elements, directly uploading the file via selenium’s native send_keys method will not work. If you don’t want to parse the input elements too much, the more direct way is to use the file upload dialog.

In general, if you need to upload a file, clicking on this element will bring up a file upload dialog asking you to select the file and click OK. This dialog is system owned, so Selenium does not directly control it. We can use the system’s automation tools or directly call the keyboard to manipulate the dialog box.

Before working on the dialog box, we first click on the file upload element via Selenium.

el = driver.find_element('id', "fileinput")
ActionChains(driver).click(el).perform()
Copy the code

Input elements are not clickable, so instead of using the element’s el.click() method, use the ActionChains click method below. The difference between them is that the element’s el.click method is more strict, checking whether the element is visible or clickable, and performing the following action after the click event is fully in effect. If these conditions are not met, an error may be reported. The Click method under Action is much rougher. It barely checks the element. It simply moves the mouse over the element and performs the click, regardless of whether the click works.

4. Use PyWinAuto to upload files

Pywinauto is an automatic tool for Windows. It can be directly retrieved from the popup box, so when the file upload window appears, you can use this tool to pass in the path of the file, and then click open button.

From PywinAuto import Desktop app = Desktop() dialog = app[' open '] # ["Button"].click() dialog["Edit"].type_keys('/path/of/file.md') # type_keys ["Button"].click()Copy the code

Another system automation tool is called PyAutoGUI. The main feature of this tool is the use of a coordinate system to locate elements, which can be easily cross-platform. It doesn’t matter if you’re Windows, MAC or Linux, you can automate it with this tool.

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 into the clipboard, and then paste it into the file path input box by CTRL + V.

5. pyautogui

Import PyperClip PyPerclip.copy ('D:\\\\ user.html ') PyAutogui.hotkey (' CTRL ', 'V ') PyAutogui.press (' Enter ', presses=2)Copy the code

The keyboard

keyboard.write('C:\\\\Users\\\\muji\\\\Desktop\\\\avatar.png')
time.sleep(1)
keyboard.press('enter')
Copy the code

Note: Baidu has disabled crawler for tutu Search, so it will prompt “failed to upload picture, please upload again” when uploading files.

6. Concurrency problems

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

I am nine handle, thank you for your patience to read, see you next time.