Stale Element in Selenium

95 views
Skip to first unread message

Jay Whonder

unread,
Sep 15, 2019, 8:18:22 PM9/15/19
to Selenium Users
Traceback (most recent call last):
  File "C:\Users\InstaPy\quickstart_templates\pelolatinomasivonew.py", line 81, in <module>
    'lainsuperable69', 'alexandramvp', 'rosaiiriisss'], amount=200, randomize=True)
  File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\instapy\instapy.py", line 3185, in interact_user_followers
    self.user_interact_media,
  File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\instapy\instapy.py", line 2599, in interact_by_users
    self.logfolder,
  File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\instapy\comment_util.py", line 92, in comment_image
    .send_keys(comment_to_be_sent)
  File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\common\action_chains.py", line 80, in perform
    self.w3c_actions.perform()
  File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\common\actions\action_builder.py", line 76, in perform
    self.driver.execute(Command.W3C_ACTIONS, enc)
  File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: The element reference of <textarea class="Ypffh"> is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed


John Doe

unread,
Sep 16, 2019, 5:13:56 AM9/16/19
to Selenium Users
If you find a WebElement and after that DOM changes the element will be invalidated (as well as other WebElement references) and you will have to re-do the find_element function call prior to attempting to work with the elements. 

So you can call find_elements_by_class_name function like: 

my_element = driver.find_element_by_class_name("Ypffh")


However this Ypffh stuff looks a little bit dynamic so it might be a better idea to use other locator strategy 

Even better way would be going for Page Object Model Design Pattern, apart from abstraction of test code from UI-related code it provides lazy initialization for the web elements so they are being automatically "found" when they're being accessed. Check out Page Objects article for details on Python implementation of the pattern.  

Reply all
Reply to author
Forward
0 new messages