Python으로 WhatsApp 메시지를 3단계로 자동화

Jan 14 2023
.
  • Selenium 라이브러리를 사용하여 웹 브라우저를 제어하고 WhatsApp의 웹 버전(web.whatsapp.com)과 상호 작용합니다.
  • 브라우저의 개발자 도구를 사용하여 페이지에서 상호 작용해야 하는 요소(예: 메시지 입력을 위한 입력 필드, 메시지 전송을 위한 버튼)를 찾습니다.
  • Selenium을 사용하여 메시지를 자동으로 보내기 위해 수동으로 수행하는 작업(예: 입력 필드에 텍스트 입력, 보내기 버튼 클릭)을 시뮬레이트합니다.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

# Create a new instance of the Firefox driver
driver = webdriver.Firefox()

# Go to WhatsApp Web
driver.get("https://web.whatsapp.com/")

# Wait for the user to scan the QR code
input("Press enter after scanning QR code")

# Find the search box and click it
search_box = driver.find_element_by_xpath('//*[@id="side"]/div[1]/div/label/div/div[2]')
search_box.click()

# Send the contact name or number
search_box.send_keys("Name or Number")
search_box.send_keys(Keys.RETURN)

# Find the message input box
message_box = driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')

# Send the message
message_box.send_keys("Your message")
message_box.send_keys(Keys.RETURN)

# Close the driver
driver.close()