2

im trying to learn selenium by making a bot that goes to an instagram account and comments on a post

this is my code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

site = webdriver.Edge('C:\\P\\Automation\\MicrosoftWebDriver')
site.get('https://www.instagram.com/example_account/')

ref = site.find_element_by_xpath("//*[@id=\"reactroot\"]/section/main/div/div/article/div[1]/div/div[1]/div[1]/a")
site.get(ref.get_attribute('href'))

txt = site.find_element_by_xpath('//*[@id="react-root"]/section/main/div/div/article/div[2]/section[3]/form/textarea')
txt.send_keys('test')
txt.send_keys(Keys.ENTER)

the problem is that when i send keys to the comment element

<textarea aria-label="Add a comment…" placeholder="Add a comment…" class="Ypffh" autocomplete="off" autocorrect="off" style="height: 18px;"></textarea>

it does nothing the first i try to send keys time and the second time i run the command:

txt.send_keys('test')

it gives me an error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 479, in send_keys
'value': keys_to_typing(value)})
  File "C:\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
    return self._parent.execute(command, params)
  File "C:\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
    self.error_handler.check_response(response)
  File "C:\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: Stale element reference

would love some help, thanks!

2
  • Why you are using send.Enter after sending text. in textarea Commented Jul 8, 2018 at 10:02
  • the idea is that after the code types the text in the textarea it will press the enter key to post the comment Commented Jul 8, 2018 at 10:05

3 Answers 3

4

Before sendkeys() use click() method to click inside textarea and then try inserting text.

Sign up to request clarification or add additional context in comments.

1 Comment

i tried it and the same things happen. i think that they might have done something that prevents bots from posting comments... maybe the element changes or something...
3

based in your solution what worked for me was calling the text area with a different selector:

            textarea = site.find_element_by_xpath('//textarea')
            textarea.click()
            textarea2 = site.find_element_by_tag_name('textarea')
            textarea2.send_keys('hi!')

(same selector didn't work)

Comments

1

alright i found the problem, the problem was that when sending keys to the textarea the element changes from this:

<textarea class="Ypffh" aria-label="Add a comment…" placeholder="Add a comment…" autocomplete="off" autocorrect="off"></textarea>

to this:

<textarea class="Ypffh" style="height: 19px;" aria-label="Add a comment…" placeholder="Add a comment…" autocomplete="off" autocorrect="off"></textarea>

and thats why the

Stale element reference

error appeared.

my solution is to select the element, send keys and then select it again and send keys again like so:

txt = site.find_element_by_class_name('Ypffh')
txt.send_keys('test')
txt = site.find_element_by_class_name('Ypffh')
txt.send_keys('test')
txt.send_keys(Keys.ENTER)

(i changed the way to find the element because finding it by class name looks much better in the code)

if anyone has a better solution i will be happy to hear it!

2 Comments

yes, there are better ways to do it, could you please provide the navigation steps. I tried with your absolute xpath , but seems like I am struggling to perform navigation. after going to instagram.com/example_account , I am clicking on log in link , then I am lost.
well i wrote instagram.com/example_account just as an example haha. my intention was that it should work for any account. you can try with your own account for example or instagram.com/instagram for instance. now, my navigation steps are like so: i open the last photo of the account in a new tab, then i inspect the comment box. and you should see there a form element and inside it a textarea element. then i copied the elements xpath (or in my latest answer i searched it by class name). the first xpath in my code is for the latest post and the second for the comment area

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.