Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from selenium import webdriver
- from selenium.webdriver.firefox.service import Service
- from selenium.webdriver.common.by import By
- from selenium.webdriver.common.keys import Keys
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.support import expected_conditions as EC
- # Set up WebDriver with Service
- service = Service('/snap/bin/geckodriver')
- driver = webdriver.Firefox(service=service)
- # Access WhatsApp Web
- driver.get('https://web.whatsapp.com')
- print("Scan the QR code to log in to WhatsApp Web.")
- # Wait until the chat search element is available (indicating successful login)
- try:
- WebDriverWait(driver, 60).until(
- EC.presence_of_element_located((By.XPATH, '//div[@contenteditable="true"][@data-tab="3"]'))
- )
- print("Login successful!")
- except Exception as e:
- print("Error detecting login. Check the QR code.")
- driver.quit()
- exit()
- # Group name and message
- group_name = "Minhas paradas"
- message = "Hello, this is an automation test on WhatsApp!"
- # Search for the group
- search_box = driver.find_element(By.XPATH, '//div[@contenteditable="true"][@data-tab="3"]')
- search_box.click()
- search_box.send_keys(group_name)
- # Wait for the group to appear in the results and click on it
- try:
- WebDriverWait(driver, 10).until(
- EC.presence_of_element_located((By.XPATH, f'//span[@title="{group_name}"]'))
- )
- group = driver.find_element(By.XPATH, f'//span[@title="{group_name}"]')
- group.click()
- except Exception as e:
- print(f"Error locating the group: {e}")
- driver.quit()
- exit()
- # Wait until the message box is visible and send the message
- try:
- message_box = WebDriverWait(driver, 10).until(
- EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div/div/div[3]/div/div[4]/div/footer/div[1]/div/span/div/div[2]/div[1]'))
- )
- message_box.click()
- # Send the message
- message_box.send_keys(message)
- message_box.send_keys(Keys.ENTER)
- print(f"Message sent to the group '{group_name}'.")
- except Exception as e:
- print(f"Error sending the message: {e}")
- # Close the browser
- driver.quit()
Add Comment
Please, Sign In to add comment