1

I am just a beginner for programming. Actually, I try to make an automatically comment on Instagram. However, if I run the code below via chrome/firefox. it will contain difference error

import time
import random
import re
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException
from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

class Commenter:

    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.driver = webdriver.Chrome()

    """closing browser"""
    def closeBrowser(self):
        self.driver.close()

    """login in to Instagram"""
    def login(self):
        driver = self.driver
        driver.get("https://www.instagram.com/")
        time.sleep(2)
        login_button = driver.find_element_by_xpath("//a[@href='/accounts/login/?source=auth_switcher']")
        login_button.click()
        time.sleep(2)
        user_name_elem = driver.find_element_by_xpath("//input[@name='username']")
        user_name_elem.clear()
        user_name_elem.send_keys(self.username)
        passworword_elem = driver.find_element_by_xpath("//input[@name='password']")
        passworword_elem.clear()
        passworword_elem.send_keys(self.password)
        passworword_elem.send_keys(Keys.RETURN)
        time.sleep(2)


    """write comment in text area using lambda function"""
    def write_comment(self, comment_text):
        try:
            comment_button = lambda: self.driver.find_element_by_link_text('Comment')
            comment_button().click()
        except NoSuchElementException:
            pass
##Add a comment…
        try:
            comment_box_elem = lambda: self.driver.find_element_by_xpath("//textarea[@aria-label='留言⋯⋯']")
            comment_box_elem().send_keys('')
            comment_box_elem().clear()
            for letter in comment_text:
                comment_box_elem().send_keys(letter)
                time.sleep((random.randint(1, 7) / 30))

            return comment_box_elem

        except StaleElementReferenceException and NoSuchElementException as e:
            print(e)
            return False

`com = Commenter(username='your login', password='your pw')
com.login()
time.sleep(3)
com.driver.get("https://www.instagram.com/p/ByhuUY3nmEJ/")
time.sleep(5)
com.write_comment("123123123123123dd")`

it is hoped that I can write a comment on the post via Firefox/ chrome.

When I use Firefox, it will occur "Element is not reachable by keyboard Instagram"

While I use Chrome, it will occur " selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable (Session info: chrome=75.0.3770.80) (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 6.1.7601 SP1 x86_64)"

It is highly appreciated that if you may give a solution to resolve my concern. I have searched the relevant questions, but I still did not get any idea to fix this. Thank you for your help.

jim
  • 11
  • 1
  • @DebanjanB, I think that the question is not duplicate, if possible please remove the duplicate tag, as I want to post an answer. But to fix this problem, you need to make comment box visible by clicking on it (just like in real life!). Add `time.sleep(5)` Before the comment, then add `comment_box_elem().click()` after the comment, I have tested the above would work – Ng Sek Long Jun 12 '19 at 09:40
  • First of all you spoke about many things in the same time _Element is not reachable by keyboard_ , _ElementNotInteractableException_ etc. Please [edit the question](/posts/56536961/edit) to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the [How to Ask](https://stackoverflow.com/help/how-to-ask) page for help clarifying this question. As of now I have added a duplicate target of each of the errors. Let me know if you still think otherwise. – undetected Selenium Jun 12 '19 at 09:43

0 Answers0