Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jimmykhlam/32c59bccb5379386c35299cb71cec247 to your computer and use it in GitHub Desktop.
Save jimmykhlam/32c59bccb5379386c35299cb71cec247 to your computer and use it in GitHub Desktop.
#define a helper function
def click_modal_button(button_text):
modal_button = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//div[@data-animate-modal-body='true']//div[@role='button']//div[text() = '%s']" % (button_text))))
modal_button.click()
#define a function that adds contact_to_add to group_name
def add_contact_to_group(group_name, contact_to_add):
#find chat with the correct title
el_target_chat = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//span[@title='%s']" % (group_name))))
el_target_chat.click()
#wait for it to load by detecting that the header changed with the new title
el_header_title = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='main']//header//span[@title='%s']" % (group_name))))
#click on the menu button
el_menu_button = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='main']//div[@data-testid='conversation-menu-button']")))
el_menu_button.click()
#click on the Group Info button
el_group_info = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='app']//li//div[@aria-label='Group info']")))
el_group_info.click()
#click on the Add Participant button
el_add_participant = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//div[@data-testid='drawer-right']//div[text() = 'Add participant']")))
el_add_participant.click()
#click on the Search
el_modal_popup = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//div[@data-animate-modal-body='true']")))
el_modal_popup.find_element(By.XPATH, "//div[contains(@title, 'Search')]").send_keys(contact_to_add)
#click on the Contact
el_contact_to_add = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//div[@data-animate-modal-body='true']//span[@title='%s']" % (contact_to_add))))
el_contact_to_add.click()
#check whether already added
if len(el_modal_popup.find_elements(By.XPATH, "//div[text() = 'Already added to group']")) > 0:
print(contact_to_add + ' was already an existing participant of ' + group_name)
el_modal_popup.find_element(By.XPATH, "//header//button[@aria-label='Close']").click()
else:
#click on the Green Check Mark
el_green_check = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//div[@data-animate-modal-body='true']//div[@role='button']//span[@data-testid='checkmark-medium']")))
el_green_check.click()
#click on the Add Participant
click_modal_button('Add participant')
print(contact_to_add + ' added to ' + group_name)
start_time = datetime.now()
#loop through a provided list of chat groups to perform an action
for chat_name in list_chat_groups_test:
el_search.clear()
el_search.send_keys(chat_name)
try:
print('Attempting to add to', chat_name, ":")
add_contact_to_group(chat_name, 'Jimmy')
except Exception as exception:
print("Exception: {}".format(type(exception).__name__))
print("Exception message: {}".format(exception))
end_time = datetime.now()
print('Duration: {}'.format(end_time - start_time))
@jimmykhlam
Copy link
Author

@jimmykhlam
Copy link
Author

This is a code section from this: https://github.com/jimmykhlam/whatsapp-web-automation

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