that_one_nigga_you_k

automation

Jan 16th, 2025
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | None | 0 0
  1. from selenium import webdriver
  2. from selenium.webdriver.firefox.service import Service
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.common.keys import Keys
  5. from selenium.webdriver.support.ui import WebDriverWait
  6. from selenium.webdriver.support import expected_conditions as EC
  7.  
  8. # Set up WebDriver with Service
  9. service = Service('/snap/bin/geckodriver')
  10. driver = webdriver.Firefox(service=service)
  11.  
  12. # Access WhatsApp Web
  13. driver.get('https://web.whatsapp.com')
  14. print("Scan the QR code to log in to WhatsApp Web.")
  15.  
  16. # Wait until the chat search element is available (indicating successful login)
  17. try:
  18.     WebDriverWait(driver, 60).until(
  19.         EC.presence_of_element_located((By.XPATH, '//div[@contenteditable="true"][@data-tab="3"]'))
  20.     )
  21.     print("Login successful!")
  22. except Exception as e:
  23.     print("Error detecting login. Check the QR code.")
  24.     driver.quit()
  25.     exit()
  26.  
  27. # Group name and message
  28. group_name = "Minhas paradas"
  29. message = "Hello, this is an automation test on WhatsApp!"
  30.  
  31. # Search for the group
  32. search_box = driver.find_element(By.XPATH, '//div[@contenteditable="true"][@data-tab="3"]')
  33. search_box.click()
  34. search_box.send_keys(group_name)
  35.  
  36. # Wait for the group to appear in the results and click on it
  37. try:
  38.     WebDriverWait(driver, 10).until(
  39.         EC.presence_of_element_located((By.XPATH, f'//span[@title="{group_name}"]'))
  40.     )
  41.     group = driver.find_element(By.XPATH, f'//span[@title="{group_name}"]')
  42.     group.click()
  43. except Exception as e:
  44.     print(f"Error locating the group: {e}")
  45.     driver.quit()
  46.     exit()
  47.  
  48. # Wait until the message box is visible and send the message
  49. try:
  50.     message_box = WebDriverWait(driver, 10).until(
  51.         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]'))
  52.     )
  53.     message_box.click()
  54.  
  55.     # Send the message
  56.     message_box.send_keys(message)
  57.     message_box.send_keys(Keys.ENTER)
  58.     print(f"Message sent to the group '{group_name}'.")
  59. except Exception as e:
  60.     print(f"Error sending the message: {e}")
  61.  
  62. # Close the browser
  63. driver.quit()
  64.  
Add Comment
Please, Sign In to add comment