When we are doing automation tools, we often upload attachments, so how to automatically upload attachments, let’s take a look at the solution today
The first step, first of all, we click the upload button on the webpage casually, if you can’t find it, log in to your mailbox, there must be a button to upload attachments
The second step is to open an interface for uploading attachments.
1. We first use snipaste (this is really easy to download from the official website ) to find the clicked coordinate point, which is the input box
2. Use pyautogui to click
3. Use pyperclip to copy and paste the path of the uploaded file
4. Then I create a random folder on the desktop, and create a few files as attachments
video display
the code
import os
import pyautogui
import time
import pyperclip
time.sleep(3)for f in os.listdir(r"d:\user\桌面\新建文件夹"):
fn ='d:\\use\\桌面\新建文件夹\\'+f
pyautogui.moveTo(736,575)
time.sleep(0.8)
pyautogui.click()
pyautogui.moveTo(622,577)
time.sleep(0.5)
pyautogui.click()
pyperclip.copy(fn)# 相当如写入到剪切板
pyautogui.moveTo(622,577)
time.sleep(0.23)
pyautogui.click()
time.sleep(0.23)
pyautogui.hotkey('Ctrl','V')
pyautogui.hotkey('Enter')
Solution 2: (pyautogui version)
Here, because I opened the company's email software instead of selenium positioning, so here I manually click the upload button, and friends who need it here can rewrite it according to their own situation! !
Here there is no need to use clicks to confirm the input box
app = Desktop()
dialog = app['open'] # Find the popup window by name
dialog[“Edit”].type_keys(fn) # Enter the value in the input box
video demo
code:
from pywinauto import Desktop
import os
import pyautogui
import time
time.sleep(3)for f in os.listdir(r"d:\user\桌面\新建文件夹"):
fn ="d:\\user\\桌面\\新建文件夹\\"+f
time.sleep(2)
app = Desktop()
dialog = app['打开']# 根据名字找到弹出窗口
dialog["Edit"].type_keys(fn)# 在输入框中输入值
time.sleep(0.6)
pyautogui.hotkey('enter')
time.sleep(2)
I hope everyone has to help
A little programmer dedicated to office automation#
I've seen this, follow + like + bookmark = don't get lost! !
If you want to know more about Python office automation, please pay attention!