Created
June 27, 2020 10:10
-
-
Save rizemon/318d1b7c89d34898724e9c28ce34cb7c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from selenium import webdriver | |
from selenium.common.exceptions import WebDriverException | |
from selenium.webdriver.support.ui import WebDriverWait, Select | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.common.keys import Keys | |
from selenium.webdriver import ChromeOptions | |
from urllib.parse import quote_plus | |
from os.path import exists, isfile | |
from time import sleep | |
# CONSTANTS | |
WEBDRIVER_TIMEOUT = 50 | |
WHATSAPP_WEB = "https://web.whatsapp.com" | |
WHATSAPP_WAME_FORMAT = "https://wa.me/{}/?text={}" | |
WHATSAPP_WEB_READY_XPATH = "//div[text()='Search or start new chat']" | |
WHATSAPP_WAME_READY_XPATH_1 = "//a[text()='Continue to Chat']" | |
WHATSAPP_WAME_READY_XPATH_2 = "//a[text()='use WhatsApp Web']" | |
WHATSAPP_SEND_XPATH = "//span[@data-icon='send']" | |
SIMPLE_WA_SLEEP_DURATION = 1 | |
SIMPLE_WA_START_INSTRUCTIONS = \ | |
""" | |
On your phone: | |
1. Open WhatsApp | |
2. Click on "Settings" on the bottom right of the screen. | |
3. Navigate to "WhatsApp Web/Desktop" | |
4. Scroll down to the bottom and click on "Scan QR code" | |
5. Point your camera at the QR code. | |
6. Wait for WhatsApp Web to load. | |
----- Press any key to continue ----- | |
""" | |
class SimpleWhatsapp: | |
def __init__(self, chromedriver_path:str) -> None: | |
self.__load_driver(chromedriver_path=chromedriver_path) | |
self.__login_whatsapp() | |
def __load_driver(self, chromedriver_path:str) -> None: | |
"""Instiantate Google Chrome driver""" | |
if not isfile(chromedriver_path): | |
raise FileNotFoundError("Missing Chrome driver") | |
chrome_options = webdriver.ChromeOptions() | |
chrome_options.add_argument('--log-level=3') | |
self.driver = webdriver.Chrome(executable_path=chromedriver_path, options=chrome_options) | |
def __login_whatsapp(self) -> None: | |
"""Login into WhatsApp via QR code""" | |
self.driver.get(WHATSAPP_WEB) | |
input(SIMPLE_WA_START_INSTRUCTIONS) | |
WebDriverWait(self.driver, WEBDRIVER_TIMEOUT).until(lambda driver: driver.find_element_by_xpath(WHATSAPP_WEB_READY_XPATH)) | |
def close(self) -> None: | |
self.driver.close() | |
def __click(self, xpath:str) -> None: | |
"""Click on the element selected by the given xpath expression""" | |
inp_xpath_search = xpath | |
element = WebDriverWait(self.driver, WEBDRIVER_TIMEOUT).until(lambda driver: driver.find_element_by_xpath(inp_xpath_search)) | |
sleep(SIMPLE_WA_SLEEP_DURATION) | |
element.click() | |
def send_message(self, number:int, message:str) -> None: | |
"""Sends the given message to the given number""" | |
encoded_msg = quote_plus(message) | |
url = WHATSAPP_WAME_FORMAT.format(number, encoded_msg) | |
self.driver.get(url) | |
self.__click(WHATSAPP_WAME_READY_XPATH_1) | |
self.__click(WHATSAPP_WAME_READY_XPATH_2) | |
self.__click(WHATSAPP_SEND_XPATH) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment