Get into the habit of writing together! This is the 4th day of my participation in the "Nuggets Daily New Plan·April Update Challenge", click to view the details of the event .
Selenium encapsulates out-of-the-box file upload operations. But with the development of modern front-end frameworks, there are more and more ways to upload files. For some file upload controls, it will be more complicated to automate control. This article mainly discusses how to automate file uploads in complex situations.
1. The input element uploads the 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 in the page, you can upload the file through the send_keys method of selenium, and 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. The input element is 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.visibility=\\'visible\\'', el)
el.send_keys(r'C:\\Users\\muji\\Desktop\\avatar.png')
复制代码
For example, Baidu's image search 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, uploading files directly through the send_keys method that comes with selenium will not succeed. If you don't want to do too much analysis on the input element, the more direct way is to use the file upload dialog to handle it.
Generally speaking, 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 control it directly. We can use the system's automated tools or directly invoke the keyboard to operate this dialog box.
Before operating the dialog, first we click on the element of the file upload through selenium.
el = driver.find_element('id', "fileinput")
ActionChains(driver).click(el).perform()
复制代码
The input element cannot be clicked, so the el.click() method of the element cannot be used, and the click method under ActionChains needs to be used. The difference between them is that the el.click method of the element is more strict, and it will detect whether the element is visible and clickable. After the click event is fully effective, the following operations are performed. If these conditions are not met, an error may be reported. The click method under Action is much more rude. It hardly detects the element, directly moves the mouse over the element, and performs the click operation. As for whether the click takes effect, it doesn't matter at all.
4. Use pywinauto to upload files
pywinauto is an automation tool under the Windows system, it 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 then click the Open button.
from pywinauto import Desktop
app = Desktop()
dialog = app['打开'] # 根据名字找到弹出窗口
dialog["Edit"].type_keys('/path/of/file.md') # 在输入框中输入值
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, which can easily be cross-platform. Whether you are Windows, Mac or Linux, you can use this tool to automate.
However, this tool does not currently support Chinese input, 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:\\\\用户.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 has disabled the crawler for image search, so when uploading a file, it will prompt "image upload failed, please re-upload".
6. Concurrency issues
Uploading files through the system window is simple and rude, but when your program needs to be executed concurrently, it is more troublesome to use this method to upload files. 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 Jiushan, thank you for your patient reading, see you next time.