python-3.x - 如何使用 Python 在 Whatsapp 上自动发送 mp3 文件
问题描述
下面的代码使用 pyautogui 库自动将 mp3 文件发送给 WhatsApp 联系人。它确实适用于 Windows 10。使用的浏览器是 Brave。
pyautogui 库可以对鼠标的移动进行编程。根据您电脑的屏幕大小和您使用的浏览器,您将不得不更改鼠标定位的 x 、 y 像素坐标(pyautogui.click(x, y) 函数。获得正确的坐标非常容易:您使用函数 pyautogui.position() 获取当前鼠标 x, y 坐标。要了解如何使用 pyautogui.position(),您可以观看此短视频:https ://www.youtube.com/watch?v=xVR -8xuBp3k&t=38s
这是代码,需要 pip install pyautogui。
import webbrowser
import pyautogui
import time
# The browser used is Brave on Windows 10. According to your PC,
# you may have to alter the click() coordinates. Watch this video
# to understand how pyautogui is used to get the mouse position:
# https://www.youtube.com/watch?v=xVR-8xuBp3k&t=38s
#
# Then, use pyautogui_get_position.py to get the mouse positions ...
whatsAppUrl = 'https://web.whatsapp.com/'
contactTargetName = '822' # part of target phone number
message = 'Hello, I send you an audio file ...'
attachmentFilePathName = r'D:\Users\Jean-Pierre\Downloads\Audiobooks\Various\Wear a mask. Help slow the spread of Covid-19._1.mp3'
webbrowser.open(whatsAppUrl)
time.sleep(5)
# click on contact search field
pyautogui.click(152, 247)
# select the targer contact
pyautogui.typewrite(contactTargetName)
time.sleep(2)
# activate the targer contact
pyautogui.click(202, 468)
time.sleep(1)
# send text message before sending the audio file
pyautogui.click(988, 1395)
pyautogui.typewrite(message)
pyautogui.click(2105, 1390)
# click on attachment icon
pyautogui.click(894, 1385)
# click on photo/video icon. Sending audio works if it is sent as a photo
# or video !
time.sleep(0.5)
pyautogui.click(894, 1268)
# enter attachment file path name
time.sleep(0.5)
pyautogui.typewrite(attachmentFilePathName)
# click on Open button
pyautogui.click(795,776)
# click on Send icon
print('Time sleeping for 20 seconds to make sure the audio file is uploaded ...')
time.sleep(20)
pyautogui.click(2060,1203)
解决方案
推荐阅读
- uwp - 当应用程序在后台时,UWP 会阻止一些通知
- python - NumPy 和 MATLAB 之间特征向量的微小差异
- php - 无法从 WAMP 登录 MySQL,但可以登录 MariaDB
- ruby - Ruby 程序的 ping 结果不正确
- c++ - 仅标头库中匿名命名空间的替代方案
- snakemake - 如何修复“MissingOutputException”?
- javascript - 反应组件中未定义的参数
- protractor - 如何解决量角器中 isDisplayed() 的承诺
- java - 将javafx窗口自动调整为scenebuilder首选项的宽度和高度?
- java - 根据 lastModification 时间快速排序文件