Selenium webdriver error - stale element refernce












1















So i tried to practice on my selenium skills using the website https://instagram.com



I Can Find an object using selenium & click, can not send keys to it.



So basically , i tried to automate comments on Instagram , iv'e found the "Add Comment" , successfully clicked on it , but when i tried to send keys i got an error.



CODE SECTION:



comment_picture = driver.find_elements_by_tag_name('textarea')
for l in comment_picture:
try:
print l.get_attribute("class")
l.click()
time.sleep(1)
l.send_keys('test')




ERROR SECTION:



Message: stale element reference: element is not attached to the page document
enter image description here





The expected result should be that i can comment on each photo on Instagram.
I don't want an answer. i really want to learn selenium. if someone know that i'm doing wrong , it would be great if i will get an hint and not a full answer.










share|improve this question





























    1















    So i tried to practice on my selenium skills using the website https://instagram.com



    I Can Find an object using selenium & click, can not send keys to it.



    So basically , i tried to automate comments on Instagram , iv'e found the "Add Comment" , successfully clicked on it , but when i tried to send keys i got an error.



    CODE SECTION:



    comment_picture = driver.find_elements_by_tag_name('textarea')
    for l in comment_picture:
    try:
    print l.get_attribute("class")
    l.click()
    time.sleep(1)
    l.send_keys('test')




    ERROR SECTION:



    Message: stale element reference: element is not attached to the page document
    enter image description here





    The expected result should be that i can comment on each photo on Instagram.
    I don't want an answer. i really want to learn selenium. if someone know that i'm doing wrong , it would be great if i will get an hint and not a full answer.










    share|improve this question



























      1












      1








      1


      1






      So i tried to practice on my selenium skills using the website https://instagram.com



      I Can Find an object using selenium & click, can not send keys to it.



      So basically , i tried to automate comments on Instagram , iv'e found the "Add Comment" , successfully clicked on it , but when i tried to send keys i got an error.



      CODE SECTION:



      comment_picture = driver.find_elements_by_tag_name('textarea')
      for l in comment_picture:
      try:
      print l.get_attribute("class")
      l.click()
      time.sleep(1)
      l.send_keys('test')




      ERROR SECTION:



      Message: stale element reference: element is not attached to the page document
      enter image description here





      The expected result should be that i can comment on each photo on Instagram.
      I don't want an answer. i really want to learn selenium. if someone know that i'm doing wrong , it would be great if i will get an hint and not a full answer.










      share|improve this question
















      So i tried to practice on my selenium skills using the website https://instagram.com



      I Can Find an object using selenium & click, can not send keys to it.



      So basically , i tried to automate comments on Instagram , iv'e found the "Add Comment" , successfully clicked on it , but when i tried to send keys i got an error.



      CODE SECTION:



      comment_picture = driver.find_elements_by_tag_name('textarea')
      for l in comment_picture:
      try:
      print l.get_attribute("class")
      l.click()
      time.sleep(1)
      l.send_keys('test')




      ERROR SECTION:



      Message: stale element reference: element is not attached to the page document
      enter image description here





      The expected result should be that i can comment on each photo on Instagram.
      I don't want an answer. i really want to learn selenium. if someone know that i'm doing wrong , it would be great if i will get an hint and not a full answer.







      python selenium






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 19 at 21:21









      Amjasd Masdhash

      385




      385










      asked Jan 19 at 18:27









      AntonioAntonio

      77




      77
























          3 Answers
          3






          active

          oldest

          votes


















          1














          stale element is because the element has been modified when you click, you have to re-find the element like this



          comment_picture = driver.find_elements_by_tag_name('textarea')
          index = 1 # xpath index start from 1
          for txt in comment_picture:
          try:
          txt.click()
          time.sleep(1)
          # re-search the textarea
          txt = driver.find_element_by_xpath('(//textarea)[%s]' % index)
          txt.send_keys('test')
          index = index + 1





          share|improve this answer


























          • Thanks learnin , but i do not fully understand your code. please explain . After some changes , iv'e got an error that said "cant focus on element." do you know this error? thanks !!!!!

            – Antonio
            Jan 20 at 19:13











          • in addition, im sure everyone would like to know how you get this answer .

            – Antonio
            Jan 20 at 19:22











          • @learnin , i see that is work works. can you please explain the code and how you get the solution ? thanks. after that we can close this case

            – Antonio
            Jan 21 at 6:21











          • it already explained, where in the code you didn't understand?

            – cieunteung
            Jan 21 at 10:37











          • how you got it? I'm not interested in a solution, I'm interested in learning selenium and learn how to troubleshoot by my self.

            – Antonio
            Jan 21 at 10:43



















          0














          element references returned by the .find_element_* functions are from the intial page load. When you click(), you are navigating away from the intial page, making all of the element references stale. You will need to call find_elements again before you send keys to the new elements.






          share|improve this answer
























          • Thanks Corey , but i don't fully understand. this performed in a for loop for all elements on the website , and i can access to the input area , but i can not send keys.

            – Antonio
            Jan 19 at 19:30











          • someone help ? please. i'm stuck on that.

            – Antonio
            Jan 19 at 20:42











          • Corey ? anything?

            – Antonio
            Jan 20 at 10:56



















          0














          You have to make sure you are signed in and able to comment.
          also executable_path='your/path'



          from selenium import webdriver

          from selenium.webdriver.common.keys import Keys
          from time import sleep
          driver = webdriver.Chrome(executable_path="/home/chromedriver")
          driver.get('https://www.instagram.com/p/Bs0y4Myg3Hk/')
          comment=driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/div/article/div[2]/section[3]/div/form/textarea')
          comment.send_keys('hello')
          comment.send_keys(Keys.RETURN)
          sleep(10)
          driver.close()





          share|improve this answer
























          • Comments are not for extended discussion; this conversation has been moved to chat.

            – meagar
            Jan 20 at 1:14











          • Works only for specified picture. the main issue that is not working on the main page on FOR loop while search each "add comment" element.

            – Antonio
            Jan 20 at 14:27











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54270119%2fselenium-webdriver-error-stale-element-refernce%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          stale element is because the element has been modified when you click, you have to re-find the element like this



          comment_picture = driver.find_elements_by_tag_name('textarea')
          index = 1 # xpath index start from 1
          for txt in comment_picture:
          try:
          txt.click()
          time.sleep(1)
          # re-search the textarea
          txt = driver.find_element_by_xpath('(//textarea)[%s]' % index)
          txt.send_keys('test')
          index = index + 1





          share|improve this answer


























          • Thanks learnin , but i do not fully understand your code. please explain . After some changes , iv'e got an error that said "cant focus on element." do you know this error? thanks !!!!!

            – Antonio
            Jan 20 at 19:13











          • in addition, im sure everyone would like to know how you get this answer .

            – Antonio
            Jan 20 at 19:22











          • @learnin , i see that is work works. can you please explain the code and how you get the solution ? thanks. after that we can close this case

            – Antonio
            Jan 21 at 6:21











          • it already explained, where in the code you didn't understand?

            – cieunteung
            Jan 21 at 10:37











          • how you got it? I'm not interested in a solution, I'm interested in learning selenium and learn how to troubleshoot by my self.

            – Antonio
            Jan 21 at 10:43
















          1














          stale element is because the element has been modified when you click, you have to re-find the element like this



          comment_picture = driver.find_elements_by_tag_name('textarea')
          index = 1 # xpath index start from 1
          for txt in comment_picture:
          try:
          txt.click()
          time.sleep(1)
          # re-search the textarea
          txt = driver.find_element_by_xpath('(//textarea)[%s]' % index)
          txt.send_keys('test')
          index = index + 1





          share|improve this answer


























          • Thanks learnin , but i do not fully understand your code. please explain . After some changes , iv'e got an error that said "cant focus on element." do you know this error? thanks !!!!!

            – Antonio
            Jan 20 at 19:13











          • in addition, im sure everyone would like to know how you get this answer .

            – Antonio
            Jan 20 at 19:22











          • @learnin , i see that is work works. can you please explain the code and how you get the solution ? thanks. after that we can close this case

            – Antonio
            Jan 21 at 6:21











          • it already explained, where in the code you didn't understand?

            – cieunteung
            Jan 21 at 10:37











          • how you got it? I'm not interested in a solution, I'm interested in learning selenium and learn how to troubleshoot by my self.

            – Antonio
            Jan 21 at 10:43














          1












          1








          1







          stale element is because the element has been modified when you click, you have to re-find the element like this



          comment_picture = driver.find_elements_by_tag_name('textarea')
          index = 1 # xpath index start from 1
          for txt in comment_picture:
          try:
          txt.click()
          time.sleep(1)
          # re-search the textarea
          txt = driver.find_element_by_xpath('(//textarea)[%s]' % index)
          txt.send_keys('test')
          index = index + 1





          share|improve this answer















          stale element is because the element has been modified when you click, you have to re-find the element like this



          comment_picture = driver.find_elements_by_tag_name('textarea')
          index = 1 # xpath index start from 1
          for txt in comment_picture:
          try:
          txt.click()
          time.sleep(1)
          # re-search the textarea
          txt = driver.find_element_by_xpath('(//textarea)[%s]' % index)
          txt.send_keys('test')
          index = index + 1






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 20 at 16:06

























          answered Jan 20 at 15:51









          cieunteungcieunteung

          3837




          3837













          • Thanks learnin , but i do not fully understand your code. please explain . After some changes , iv'e got an error that said "cant focus on element." do you know this error? thanks !!!!!

            – Antonio
            Jan 20 at 19:13











          • in addition, im sure everyone would like to know how you get this answer .

            – Antonio
            Jan 20 at 19:22











          • @learnin , i see that is work works. can you please explain the code and how you get the solution ? thanks. after that we can close this case

            – Antonio
            Jan 21 at 6:21











          • it already explained, where in the code you didn't understand?

            – cieunteung
            Jan 21 at 10:37











          • how you got it? I'm not interested in a solution, I'm interested in learning selenium and learn how to troubleshoot by my self.

            – Antonio
            Jan 21 at 10:43



















          • Thanks learnin , but i do not fully understand your code. please explain . After some changes , iv'e got an error that said "cant focus on element." do you know this error? thanks !!!!!

            – Antonio
            Jan 20 at 19:13











          • in addition, im sure everyone would like to know how you get this answer .

            – Antonio
            Jan 20 at 19:22











          • @learnin , i see that is work works. can you please explain the code and how you get the solution ? thanks. after that we can close this case

            – Antonio
            Jan 21 at 6:21











          • it already explained, where in the code you didn't understand?

            – cieunteung
            Jan 21 at 10:37











          • how you got it? I'm not interested in a solution, I'm interested in learning selenium and learn how to troubleshoot by my self.

            – Antonio
            Jan 21 at 10:43

















          Thanks learnin , but i do not fully understand your code. please explain . After some changes , iv'e got an error that said "cant focus on element." do you know this error? thanks !!!!!

          – Antonio
          Jan 20 at 19:13





          Thanks learnin , but i do not fully understand your code. please explain . After some changes , iv'e got an error that said "cant focus on element." do you know this error? thanks !!!!!

          – Antonio
          Jan 20 at 19:13













          in addition, im sure everyone would like to know how you get this answer .

          – Antonio
          Jan 20 at 19:22





          in addition, im sure everyone would like to know how you get this answer .

          – Antonio
          Jan 20 at 19:22













          @learnin , i see that is work works. can you please explain the code and how you get the solution ? thanks. after that we can close this case

          – Antonio
          Jan 21 at 6:21





          @learnin , i see that is work works. can you please explain the code and how you get the solution ? thanks. after that we can close this case

          – Antonio
          Jan 21 at 6:21













          it already explained, where in the code you didn't understand?

          – cieunteung
          Jan 21 at 10:37





          it already explained, where in the code you didn't understand?

          – cieunteung
          Jan 21 at 10:37













          how you got it? I'm not interested in a solution, I'm interested in learning selenium and learn how to troubleshoot by my self.

          – Antonio
          Jan 21 at 10:43





          how you got it? I'm not interested in a solution, I'm interested in learning selenium and learn how to troubleshoot by my self.

          – Antonio
          Jan 21 at 10:43













          0














          element references returned by the .find_element_* functions are from the intial page load. When you click(), you are navigating away from the intial page, making all of the element references stale. You will need to call find_elements again before you send keys to the new elements.






          share|improve this answer
























          • Thanks Corey , but i don't fully understand. this performed in a for loop for all elements on the website , and i can access to the input area , but i can not send keys.

            – Antonio
            Jan 19 at 19:30











          • someone help ? please. i'm stuck on that.

            – Antonio
            Jan 19 at 20:42











          • Corey ? anything?

            – Antonio
            Jan 20 at 10:56
















          0














          element references returned by the .find_element_* functions are from the intial page load. When you click(), you are navigating away from the intial page, making all of the element references stale. You will need to call find_elements again before you send keys to the new elements.






          share|improve this answer
























          • Thanks Corey , but i don't fully understand. this performed in a for loop for all elements on the website , and i can access to the input area , but i can not send keys.

            – Antonio
            Jan 19 at 19:30











          • someone help ? please. i'm stuck on that.

            – Antonio
            Jan 19 at 20:42











          • Corey ? anything?

            – Antonio
            Jan 20 at 10:56














          0












          0








          0







          element references returned by the .find_element_* functions are from the intial page load. When you click(), you are navigating away from the intial page, making all of the element references stale. You will need to call find_elements again before you send keys to the new elements.






          share|improve this answer













          element references returned by the .find_element_* functions are from the intial page load. When you click(), you are navigating away from the intial page, making all of the element references stale. You will need to call find_elements again before you send keys to the new elements.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 19 at 18:45









          Corey GoldbergCorey Goldberg

          37.2k22108123




          37.2k22108123













          • Thanks Corey , but i don't fully understand. this performed in a for loop for all elements on the website , and i can access to the input area , but i can not send keys.

            – Antonio
            Jan 19 at 19:30











          • someone help ? please. i'm stuck on that.

            – Antonio
            Jan 19 at 20:42











          • Corey ? anything?

            – Antonio
            Jan 20 at 10:56



















          • Thanks Corey , but i don't fully understand. this performed in a for loop for all elements on the website , and i can access to the input area , but i can not send keys.

            – Antonio
            Jan 19 at 19:30











          • someone help ? please. i'm stuck on that.

            – Antonio
            Jan 19 at 20:42











          • Corey ? anything?

            – Antonio
            Jan 20 at 10:56

















          Thanks Corey , but i don't fully understand. this performed in a for loop for all elements on the website , and i can access to the input area , but i can not send keys.

          – Antonio
          Jan 19 at 19:30





          Thanks Corey , but i don't fully understand. this performed in a for loop for all elements on the website , and i can access to the input area , but i can not send keys.

          – Antonio
          Jan 19 at 19:30













          someone help ? please. i'm stuck on that.

          – Antonio
          Jan 19 at 20:42





          someone help ? please. i'm stuck on that.

          – Antonio
          Jan 19 at 20:42













          Corey ? anything?

          – Antonio
          Jan 20 at 10:56





          Corey ? anything?

          – Antonio
          Jan 20 at 10:56











          0














          You have to make sure you are signed in and able to comment.
          also executable_path='your/path'



          from selenium import webdriver

          from selenium.webdriver.common.keys import Keys
          from time import sleep
          driver = webdriver.Chrome(executable_path="/home/chromedriver")
          driver.get('https://www.instagram.com/p/Bs0y4Myg3Hk/')
          comment=driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/div/article/div[2]/section[3]/div/form/textarea')
          comment.send_keys('hello')
          comment.send_keys(Keys.RETURN)
          sleep(10)
          driver.close()





          share|improve this answer
























          • Comments are not for extended discussion; this conversation has been moved to chat.

            – meagar
            Jan 20 at 1:14











          • Works only for specified picture. the main issue that is not working on the main page on FOR loop while search each "add comment" element.

            – Antonio
            Jan 20 at 14:27
















          0














          You have to make sure you are signed in and able to comment.
          also executable_path='your/path'



          from selenium import webdriver

          from selenium.webdriver.common.keys import Keys
          from time import sleep
          driver = webdriver.Chrome(executable_path="/home/chromedriver")
          driver.get('https://www.instagram.com/p/Bs0y4Myg3Hk/')
          comment=driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/div/article/div[2]/section[3]/div/form/textarea')
          comment.send_keys('hello')
          comment.send_keys(Keys.RETURN)
          sleep(10)
          driver.close()





          share|improve this answer
























          • Comments are not for extended discussion; this conversation has been moved to chat.

            – meagar
            Jan 20 at 1:14











          • Works only for specified picture. the main issue that is not working on the main page on FOR loop while search each "add comment" element.

            – Antonio
            Jan 20 at 14:27














          0












          0








          0







          You have to make sure you are signed in and able to comment.
          also executable_path='your/path'



          from selenium import webdriver

          from selenium.webdriver.common.keys import Keys
          from time import sleep
          driver = webdriver.Chrome(executable_path="/home/chromedriver")
          driver.get('https://www.instagram.com/p/Bs0y4Myg3Hk/')
          comment=driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/div/article/div[2]/section[3]/div/form/textarea')
          comment.send_keys('hello')
          comment.send_keys(Keys.RETURN)
          sleep(10)
          driver.close()





          share|improve this answer













          You have to make sure you are signed in and able to comment.
          also executable_path='your/path'



          from selenium import webdriver

          from selenium.webdriver.common.keys import Keys
          from time import sleep
          driver = webdriver.Chrome(executable_path="/home/chromedriver")
          driver.get('https://www.instagram.com/p/Bs0y4Myg3Hk/')
          comment=driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/div/article/div[2]/section[3]/div/form/textarea')
          comment.send_keys('hello')
          comment.send_keys(Keys.RETURN)
          sleep(10)
          driver.close()






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 19 at 20:58









          Amjasd MasdhashAmjasd Masdhash

          385




          385













          • Comments are not for extended discussion; this conversation has been moved to chat.

            – meagar
            Jan 20 at 1:14











          • Works only for specified picture. the main issue that is not working on the main page on FOR loop while search each "add comment" element.

            – Antonio
            Jan 20 at 14:27



















          • Comments are not for extended discussion; this conversation has been moved to chat.

            – meagar
            Jan 20 at 1:14











          • Works only for specified picture. the main issue that is not working on the main page on FOR loop while search each "add comment" element.

            – Antonio
            Jan 20 at 14:27

















          Comments are not for extended discussion; this conversation has been moved to chat.

          – meagar
          Jan 20 at 1:14





          Comments are not for extended discussion; this conversation has been moved to chat.

          – meagar
          Jan 20 at 1:14













          Works only for specified picture. the main issue that is not working on the main page on FOR loop while search each "add comment" element.

          – Antonio
          Jan 20 at 14:27





          Works only for specified picture. the main issue that is not working on the main page on FOR loop while search each "add comment" element.

          – Antonio
          Jan 20 at 14:27


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54270119%2fselenium-webdriver-error-stale-element-refernce%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Liquibase includeAll doesn't find base path

          How to use setInterval in EJS file?

          Petrus Granier-Deferre