from selenium import webdriver
from time import sleep
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait


def Dm(driver,user,message):
    ''' This function is used to direct message a single user/group '''

    driver.get('https://www.instagram.com/direct/inbox/')
    
    send_message_button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="react-root"]/section/div/div[2]/div/div/div[2]/div/div[3]/div/button'))).click()
    
    search_user = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[5]/div/div/div[2]/div[1]/div/div[2]/input')))
    search_user.send_keys(user)
    
    selector = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[5]/div/div/div[2]/div[2]/div/div/div[3]/button/span'))).click()
    
    next_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[5]/div/div/div[1]/div/div[2]/div/button/div'))).click()
    
    try:
        text = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="react-root"]/section/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div[2]/textarea')))
        text.send_keys(message)
        send = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="react-root"]/section/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div[3]/button'))).click()
        driver.get('https://www.instagram.com/direct/inbox/')
    
    except:
        print('No message sent to '+user)
        driver.get('https://www.instagram.com/direct/inbox/')
[((16, 50, 16, 148), 'selenium.webdriver.support.expected_conditions.element_to_be_clickable', 'EC.element_to_be_clickable', ({(16, 77, 16, 147): "(By.XPATH, '/html/body/div[5]/div/div/div[2]/div[1]/div/div[2]/input')"}, {}), "((By.XPATH,\n    '/html/body/div[5]/div/div/div[2]/div[1]/div/div[2]/input'))", True, 'from selenium.webdriver.support import expected_conditions as EC\n'), ((16, 18, 16, 43), 'selenium.webdriver.support.wait.WebDriverWait', 'WebDriverWait', ({(16, 32, 16, 38): 'driver', (16, 40, 16, 42): '10'}, {}), '(driver, 10)', False, 'from selenium.webdriver.support.wait import WebDriverWait\n'), ((24, 47, 24, 186), 'selenium.webdriver.support.expected_conditions.element_to_be_clickable', 'EC.element_to_be_clickable', ({(24, 74, 24, 185): '(By.XPATH,\n    \'//*[@id="react-root"]/section/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div[2]/textarea\'\n    )'}, {}), '((By.XPATH,\n    \'//*[@id="react-root"]/section/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div[2]/textarea\'\n    ))', True, 'from selenium.webdriver.support import expected_conditions as EC\n'), ((14, 58, 14, 177), 'selenium.webdriver.support.expected_conditions.element_to_be_clickable', 'EC.element_to_be_clickable', ({(14, 85, 14, 176): '(By.XPATH,\n    \'//*[@id="react-root"]/section/div/div[2]/div/div/div[2]/div/div[3]/div/button\'\n    )'}, {}), '((By.XPATH,\n    \'//*[@id="react-root"]/section/div/div[2]/div/div/div[2]/div/div[3]/div/button\'\n    ))', True, 'from selenium.webdriver.support import expected_conditions as EC\n'), ((19, 47, 19, 155), 'selenium.webdriver.support.expected_conditions.element_to_be_clickable', 'EC.element_to_be_clickable', ({(19, 74, 19, 154): "(By.XPATH, '/html/body/div[5]/div/div/div[2]/div[2]/div/div/div[3]/button/span'\n    )"}, {}), "((By.XPATH,\n    '/html/body/div[5]/div/div/div[2]/div[2]/div/div/div[3]/button/span'))", True, 'from selenium.webdriver.support import expected_conditions as EC\n'), ((21, 50, 21, 150), 'selenium.webdriver.support.expected_conditions.element_to_be_clickable', 'EC.element_to_be_clickable', ({(21, 77, 21, 149): "(By.XPATH, '/html/body/div[5]/div/div/div[1]/div/div[2]/div/button/div')"}, {}), "((By.XPATH,\n    '/html/body/div[5]/div/div/div[1]/div/div[2]/div/button/div'))", True, 'from selenium.webdriver.support import expected_conditions as EC\n'), ((24, 15, 24, 40), 'selenium.webdriver.support.wait.WebDriverWait', 'WebDriverWait', ({(24, 29, 24, 35): 'driver', (24, 37, 24, 39): '10'}, {}), '(driver, 10)', False, 'from selenium.webdriver.support.wait import WebDriverWait\n'), ((14, 26, 14, 51), 'selenium.webdriver.support.wait.WebDriverWait', 'WebDriverWait', ({(14, 40, 14, 46): 'driver', (14, 48, 14, 50): '20'}, {}), '(driver, 20)', False, 'from selenium.webdriver.support.wait import WebDriverWait\n'), ((19, 15, 19, 40), 'selenium.webdriver.support.wait.WebDriverWait', 'WebDriverWait', ({(19, 29, 19, 35): 'driver', (19, 37, 19, 39): '10'}, {}), '(driver, 10)', False, 'from selenium.webdriver.support.wait import WebDriverWait\n'), ((21, 18, 21, 43), 'selenium.webdriver.support.wait.WebDriverWait', 'WebDriverWait', ({(21, 32, 21, 38): 'driver', (21, 40, 21, 42): '10'}, {}), '(driver, 10)', False, 'from selenium.webdriver.support.wait import WebDriverWait\n'), ((26, 47, 26, 184), 'selenium.webdriver.support.expected_conditions.element_to_be_clickable', 'EC.element_to_be_clickable', ({(26, 74, 26, 183): '(By.XPATH,\n    \'//*[@id="react-root"]/section/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div[3]/button\'\n    )'}, {}), '((By.XPATH,\n    \'//*[@id="react-root"]/section/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div[3]/button\'\n    ))', True, 'from selenium.webdriver.support import expected_conditions as EC\n'), ((26, 15, 26, 40), 'selenium.webdriver.support.wait.WebDriverWait', 'WebDriverWait', ({(26, 29, 26, 35): 'driver', (26, 37, 26, 39): '10'}, {}), '(driver, 10)', False, 'from selenium.webdriver.support.wait import WebDriverWait\n')]