使用Selenium for Python向Instagram添加评论

时间:2019-07-10 14:16:50

标签: python selenium selenium-webdriver

我想使用Selenium和Python在Instagram帖子中添加评论。具体而言,页面具有以下结构:

<textarea aria-label="Aggiungi un commento..." placeholder="Aggiungi un commento..." class="Ypffh" autocomplete="off" autocorrect="off" style="height: 18px;"></textarea>

我尝试过:

comment_button = ui.WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "textarea.Ypffh")))

comment_button.send_keys("This is my comment")
comment_button.send_keys(Keys.RETURN)

但是它似乎没有用;它确实选择了注释框,但未添加文本

1 个答案:

答案 0 :(得分:0)

我还在从事一些instagram自动化方面的工作。这是我对图片发表评论的代码:

        from selenium import webdriver
        from selenium.common.exceptions import StaleElementReferenceException
        from selenium.webdriver.common.keys import Keys
        from selenium.webdriver.common.by import By
        from selenium.webdriver.support import expected_conditions as EC
        from selenium.webdriver.support import ui

        commentSection = ui.WebDriverWait(self.driver, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, "textarea.Ypffh")))
        self.driver.execute_script("arguments[0].scrollIntoView(true);", 
        commentSection)
        while(1 == 1):
            try:
                commentSection = ui.WebDriverWait(self.driver, 10).until(
                    EC.element_to_be_clickable((By.CSS_SELECTOR, "textarea.Ypffh")))
                comment = comments[random.randint(0,numOfComments)]
                commentSection.send_keys(comment)
                commentSection.send_keys(Keys.ENTER)
                time.sleep(random.randint(3,4))
                break
            except Exception:
                time.sleep(random.randint(5,6))

如果需要,可以随时查看我的github存储库以获得更多帮助! https://github.com/atloftus/IGActionAutomator