Skip to content

Instantly share code, notes, and snippets.

@WittmannF
Created November 14, 2018 21:34
Show Gist options
  • Save WittmannF/292bf7634adab24445e1006025c039e8 to your computer and use it in GitHub Desktop.
Save WittmannF/292bf7634adab24445e1006025c039e8 to your computer and use it in GitHub Desktop.
WhatsApp API Selenium
"""
### Implemented Methods
- Get contacts from a selected group
### Requires
- Selenium: `pip install selenium`
- ChromeDriver: http://chromedriver.chromium.org/
- After downloading chromedriver, make sure to add in a folder accessible from the PATH
### Example of Usage:
In [1]: from whatsapp_api import WhatsApp
In [2]: wp = WhatsApp()
Loading...
Please scan the QR Code and enter in the group
In [3]: wp.get_contacts()
Out[3]:
[u'+1...
"""
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# Whatsapp Parameters
WP_LINK = 'https://web.whatsapp.com'
CONTACTS_XPATH = '//*[@id="main"]/header/div[2]/div[2]/span'
class WhatsApp:
def __init__(self):
self.driver = self._setup_driver(self)
self.driver.get(WP_LINK)
print("Please scan the QR Code and enter in the group")
@staticmethod
def _setup_driver(self):
print('Loading...')
chrome_options = Options()
chrome_options.add_argument("disable-infobars")
driver = webdriver.Chrome(chrome_options=chrome_options)
return driver
def get_contacts(self):
el = self.driver.find_element_by_xpath(CONTACTS_XPATH)
return el.text.split(',')
@WittmannF
Copy link
Author

Implemented Methods

  • Get contacts from a selected group

Requires

  • Selenium: pip install selenium
  • ChromeDriver: http://chromedriver.chromium.org/
    • After downloading chromedriver, make sure to add in a folder accessible from the PATH

Example of usage:

In [1]: from whatsapp_api import WhatsApp
In [2]: wp = WhatsApp()
Loading...
Please scan the QR Code and enter in the group
In [3]: wp.get_contacts()
Out[3]:
[u'+1...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment