0

I am trying to send a direct message to the usernames saved in another file. But my code is not locating the message button on Instagram.

I've tried all possible ways to locate it (By Xpath, classname, tag_name, css_selector), but I'm still getting the same error.

Here is my code below:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time, random

my_username = "of*********_8"
my_password = "*************"

text_path= '/Users/Admin/Desktop/links.txt'
# opening file containing links
with open(text_path) as file:
    links = file.readlines()


# Messages:
messages = ['Hello, Good Morning!!!']

# Delay time between messages in sec:
between_messages = 2000

browser = webdriver.Chrome('chromedriver')

# Authorization:
def auth(username, password):
    try:
        browser.get('https://instagram.com')
        time.sleep(random.randrange(2,4))

        input_username = browser.find_element(By.NAME, 'username')
        input_password = browser.find_element(By.NAME, 'password')

        input_username.send_keys(username)
        time.sleep(random.randrange(1,2))
        input_password.send_keys(password)
        time.sleep(random.randrange(1,2))
        input_password.send_keys(Keys.ENTER)

    except Exception as err:
        print(err)
        browser.quit()

# Sending messages:
def send_message(users, messages):
    try:
        browser.find_element(By.XPATH, '/html/body/div[1]/section/nav/div[2]/div/div/div[3]/div/div[2]/a').click()
        time.sleep(random.randrange(3,5))
        browser.find_element(By.XPATH, '/html/body/div[5]/div/div/div/div[3]/button[2]').click()
        time.sleep(random.randrange(1,2))
        browser.find_element(By.XPATH, '/html/body/div[1]/section/div/div[2]/div/div/div[2]/div/div[3]/div/button').click()
        for user in users:
            time.sleep(random.randrange(1,2))
            browser.find_element(By.XPATH, '/html/body/div[5]/div/div/div[2]/div[1]/div/div[2]/input').send_keys(users)
            time.sleep(random.randrange(2,3))
            browser.find_element(By.XPATH, '/html/body/div[5]/div/div/div[2]/div[2]/div[1]').find_element(By.TAG_NAME, 'button').click()
            time.sleep(random.randrange(3,4))
            browser.find_element(By.XPATH, '/html/body/div[5]/div/div/div[1]/div/div[2]/div/button').click()
            time.sleep(random.randrange(3,4))
            text_area = browser.find_element(By.XPATH, '/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div[2]/textarea')
            text_area.send_keys(messages)
            time.sleep(random.randrange(2,4))
            text_area.send_keys(Keys.ENTER)
            print(f'Message successfully sent to {user}')
            time.sleep(between_messages)
            browser.find_element(By.XPATH, '/html/body/div[1]/section/div/div[2]/div/div/div[1]/div[1]/div/div[3]/button').click()

    except Exception as err:
        print(err)
        browser.quit()


auth(my_username, my_password)
time.sleep(random.randrange(2,4))
send_message(links, messages)

Not sure what I'm missing here.

Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24
  • It'd be helpful if you could also share the error you are getting while running the attached code. – mah Apr 07 '23 at 06:37
  • Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/section/nav/div[2]/div/div/div[3]/div/div[2]/a"} (Session info: chrome=111.0.5563.147) – Winterfell Kingston Apr 07 '23 at 10:16
  • Try this XPATH: `/html/body/div[2]/div/div/div[1]/div/div/div/div[1]/section/main/div/header/section/div[1]/div[2]/div/div[2]` – mah Apr 11 '23 at 05:32

0 Answers0