Send a Instagram comment using Python with Selenium


I want to submit a comment using Python with Selenium. The comment box in Instagram web looks like this:

<textarea aria-label="Añade un comentario..." placeholder="Añade un comentario..." class="Ypffh" autocomplete="off" autocorrect="off" style="height: 18px;"></textarea>

My Python code:

coment_box = driver.find_elements_by_css_selector("form textarea") 
coment_box.send_keys("Nice picture")

I tried to use the find_by_xpath("here_xpath") but it returns me an error saying that: AttributeError: ‘list’ object has no attribute ‘send_keys’.

Asked By: user10706210



Try to use the following code:

from import ui
from import expected_conditions as EC
from import By

comment_box = ui.WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "textarea.Ypffh")))
driver.execute_script("arguments[0].scrollIntoView(true);", comment_box)

Hope it helps you!

Answered By: Ratmir Asanov

Was having this problem myself, although this post is old I found a solution so if anyone else is stuck here is what worked for me:

commentArea = driver.find_element_by_class_name('Ypffh')
commentArea = driver.find_element_by_class_name('Ypffh')
commentArea.send_keys("YOUR COMMENT HERE...")

I believe it has something to do with how instagram updates the textArea after you click on it, but this solution worked for me after searching and lots of trial and error 🙂

Answered By: Carter4502

I clicked two times. it worked. I mean that you have to type bellow codes:(2 times write the click line)

commentArea = driver.find_element_by_class_name('Ypffh')
commentArea = driver.find_element_by_class_name('Ypffh')
commentArea.send_keys("YOUR COMMENT HERE...")

Use the following code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import re
import time
import datetime
from import WebDriverWait
from import expected_conditions as EC
from import By
import threading

def comment(browser):
    username = browser.find_element_by_name("username")
    username.send_keys('YOUR USERNAME')
    passw = browser.find_element_by_name("password")
    passw.send_keys('YOUR PASSWORD')
    for i in range(5):
        browser.get('POST LINK')
        commentArea = browser.find_element_by_class_name('Ypffh')
        commentArea = browser.find_element_by_class_name('Ypffh')
        commentArea.send_keys("Using selenium to comment in burst of 5 ")
if __name__ == '__main__':
    browser1 = webdriver.Chrome()
    browser2 = webdriver.Chrome()
    threading.Thread(target=comment, args=[browser1]).start()
    threading.Thread(target=comment, args=[browser2]).start()
    # comment(browser)

This also has threading so you can post multiple comments at a time just enter your username and password and the link of post on which you want to comment

Answered By: Aryan Kapoor
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.