user10706210
user10706210

Reputation:

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'.

Upvotes: 1

Views: 6432

Answers (4)

Aryan Kapoor
Aryan Kapoor

Reputation: 1

Use the following code:

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


def comment(browser):
    browser.get('https://www.instagram.com/')
    time.sleep(5)
    username = browser.find_element_by_name("username")
    username.send_keys('YOUR USERNAME')
    passw = browser.find_element_by_name("password")
    passw.send_keys('YOUR PASSWORD')
    passw.send_keys(Keys.RETURN)
    time.sleep(40)
    for i in range(5):
        browser.get('POST LINK')
        commentArea = browser.find_element_by_class_name('Ypffh')
        commentArea.click()
        time.sleep(5)
        commentArea = browser.find_element_by_class_name('Ypffh')
        commentArea.click()
        commentArea.send_keys("Using selenium to comment in burst of 5 ")
        commentArea.send_keys(Keys.RETURN)
        time.sleep(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

Upvotes: 0

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.click()
sleep(5)
commentArea = driver.find_element_by_class_name('Ypffh')
commentArea.click()
commentArea.send_keys("YOUR COMMENT HERE...")

Upvotes: 6

Carter4502
Carter4502

Reputation: 157

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.click()
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 :)

Upvotes: 2

Ratmir Asanov
Ratmir Asanov

Reputation: 6459

Try to use the following code:

from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by 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)
comment_box.send_keys("Hello!")

Hope it helps you!

Upvotes: 1

Related Questions