Understanding Selenium And Integration With Ralph | by Hasanudin | Nov, 2024
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
# Configuration
WHATSAPP_GROUP_NAME = “License Reminder” # Replace with your group name
CHROMEDRIVER_PATH = “/path/to/chromedriver”
def send_whatsapp_reminder(licenses):
driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH)
try:
# Open WhatsApp Web
driver.get(“https://web.whatsapp.com/”)
print(“Waiting for WhatsApp Web to load…”)
time.sleep(15) # Wait for manual QR code scanning if needed
# Search for the group
search_box = driver.find_element(By.XPATH, “//div[@contenteditable=’true’]”)
search_box.click()
search_box.send_keys(WHATSAPP_GROUP_NAME)
time.sleep(2)
search_box.send_keys(Keys.RETURN)
# Prepare the message
message = “📌 *License Expiry Reminder*\n\nThe following licenses are expiring soon:\n”
for license in licenses:
message += (
f”• Name: {license[‘name’]}\n”
f” Expiry Date: {license[‘valid_to’]}\n”
f” Owner: {license[‘owner’]}\n\n”
)
message += “Please take necessary action. ✅”
# Send the message
message_box = driver.find_element(By.XPATH, “//div[@contenteditable=’true’ and @data-tab=’10’]”)
message_box.click()
message_box.send_keys(message)
message_box.send_keys(Keys.RETURN)
print(“Reminder sent successfully!”)
finally:
# Close the browser
driver.quit()
licenses = get_licenses_near_expiry(days=30)
if licenses:
send_whatsapp_reminder(licenses)
else:
print(“No licenses nearing expiry.”)
Read more here: Source link