简体   繁体   中英

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

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...")

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 :)

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!

post_link = "  " #link to your post
        bot.get(post_link)
        entry = lambda: bot.find_element_by_xpath("//textarea[@aria-label='Add a comment…']")
        entry().click()
        entry().clear()
        entry().send_keys("hello bot")
        entry().send_keys(Keys.ENTER)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM