【问题标题】:Send a Instagram comment using Python with Selenium使用 Python 和 Selenium 发送 Instagram 评论
【发布时间】:2018-11-27 14:44:59
【问题描述】:

我想使用带有 Selenium 的 Python 提交评论。 Instagram 网页中的评论框如下所示:

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

我的 Python 代码:

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

我尝试使用find_by_xpath("here_xpath"),但它返回一个错误,提示:AttributeError: 'list' object has no attribute 'send_keys'。

【问题讨论】:

  • 您可以使用以下 CSS 选择器:.Ypffh.
  • 它说 AttributeError: 'list' object has no attribute 'send_keys'
  • 所以,你需要改用find_element_by_css_selector
  • 如果我使用comment_box.find_element_by_css_selector(".Ypffh") 和comment_box.send_keys("Hello") 返回之前提到的错误
  • 不可能,因为find_element_by_css_selector会返回1个网页元素。

标签: python html selenium instagram


【解决方案1】:

我点击了两次。有效。我的意思是你必须输入下面的代码:(2次写点击线)

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

【讨论】:

    【解决方案2】:

    我自己也有这个问题,虽然这篇文章很旧,但我找到了一个解决方案,所以如果其他人被困在这里,这对我有用:

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

    我相信这与你点击它后 instagram 如何更新 textArea 有关,但这个解决方案在搜索和大量试验和错误后对我有用:)

    【讨论】:

      【解决方案3】:

      尝试使用以下代码:

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

      希望对你有帮助!

      【讨论】:

      • 评论框似乎被点击了
      • 它显示了这个错误(但是评论框看起来被点击了)= StaleElementReferenceException: Message: stale element reference: element is not attach to the page document (Session info: chrome=70.0.3538.102) (Driver info : chromedriver=2.44.609538 (b655c5a60b0b544917107a59d4153d4bf78e1b90),platform=Windows NT 10.0.17134 x86_64)
      • @AlejandroDapena,请再试一次我更新的答案。也许,您需要更新驱动程序和浏览器的版本。
      • WebDriverException:消息:未知错误:无法聚焦元素(会话信息:chrome=70.0.3538.102)(驱动程序信息:chromedriver=2.44.609538(b655c5a60b0b544917107a59d4153d4bf78e1b90),平台=Windows NT86644)
      • 我正在使用 chromedriver.exe(idk 如果那是最好的/它已更新)
      【解决方案4】:

      使用以下代码:

      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)
      

      这也有线程,因此您可以一次发布多个 cmet,只需输入您的用户名和密码以及您要评论的帖子链接

      【讨论】:

        猜你喜欢
        • 2020-02-04
        • 2019-04-28
        • 1970-01-01
        • 1970-01-01
        • 2021-03-09
        • 1970-01-01
        • 2021-06-30
        • 2021-04-03
        • 1970-01-01
        相关资源
        最近更新 更多