Search code examples
pythonhtmlseleniuminstagram

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


Solution

  • 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!