- Mark as New
- Bookmark Message
- Subscribe to Message
- Mute Message
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
selenium.common.exceptions.StaleElementReferenceException: Message: Element is no longer valid
I have a Python/Selenium script that has 30+ of the following. I've included two samples.
try: xpathName = "//*[@id='locationDeductableAmount']" xpathValue = "1000" wait = WebDriverWait(driver, 10) element = wait.until(EC.element_to_be_clickable((By.XPATH, "" + xpathName + ""))) element.click() dropdown_element = wait.until(EC.presence_of_element_located((By.XPATH, "" + xpathName + ""))) select = Select(dropdown_element) select.select_by_visible_text(xpathValue) except: stop = time.time() print("Step " + step + ": Response Time (sec) is " + str(stop - start)) raise Exception ("WARNING: Could not find " + xpathName + " id") print("--------") try: xpathName = "//*[@id='buildingSquareFootagerest']" xpathValue = "3000" wait = WebDriverWait(driver, 10) element = wait.until(EC.element_to_be_clickable((By.XPATH, "" + xpathName + ""))) element.click() element = wait.until(EC.element_to_be_clickable((By.XPATH, "" + xpathName + ""))) element.clear() element = wait.until(EC.element_to_be_clickable((By.XPATH, "" + xpathName + ""))) element.send_keys(xpathValue) except: stop = time.time() print("Step " + step + ": Response Time (sec) is " + str(stop - start)) raise Exception ("WARNING: Could not find " + xpathName + " id") print("--------")
The website in question only works in IE.
Sometimes my script completes without any issues and other times it errors with "selenium.common.exceptions.StaleElementReferenceException: Message: Element is no longer valid".
This error which is random happens at different areas of the script.
I've tried using a hard pause of 1 or 2 seconds. There are times it works and other times it fails.
Can anyone please tell me what I need to update to make this work?
I've update the post to include the exception messages
Traceback (most recent call last): File "c:\Users\uswarv41\Box Sync\Documents\GitHub\appd-selenium-scripts\CML_BIE_PROD_NB_Restaurant_Synthetic_Script.py", line 375, in <module> element.click() File "C:\Users\uswarv41\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click self._execute(Command.CLICK_ELEMENT) File "C:\Users\uswarv41\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute return self._parent.execute(command, params) File "C:\Users\uswarv41\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response) File "C:\Users\uswarv41\AppData\Local\Programs\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: Element is no longer valid During handling of the above exception, another exception occurred: Traceback (most recent call last): File "c:\Users\uswarv41\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\ptvsd_launcher.py", line 45, in <module> main(ptvsdArgs) File "c:\Users\uswarv41\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\__main__.py", line 265, in main wait=args.wait) File "c:\Users\uswarv41\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\__main__.py", line 256, in handle_argse_args run_main(addr, name, kind, *extra, **kwargs) File "c:\Users\uswarv41\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_local.py", line 52, in run_main runner(addr, name, kind == 'module', *extra, **kwargs) File "c:\Users\uswarv41\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\runner.py", line 32, in run set_trace=False) File "c:\Users\uswarv41\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\pydevd.py", line 1283, in run return self._exec(is_module, entry_point_fn, module_name, file, globals, locals) File "c:\Users\uswarv41\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\pydevd.py", line 1290, in _exec pydev_imports.execfile(file, globals, locals) # execute the script File "c:\Users\uswarv41\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\_pydev_imps\_pydev_execfile.py", line 25, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "c:\Users\uswarv41\Box Sync\Documents\GitHub\appd-selenium-scripts\CML_BIE_PROD_NB_Restaurant_Synthetic_Script.py", line 385, in <module> raise Exception ("WARNING: Could not find " + xpathName + " id") Exception: WARNING: Could not find //*[@id='locationDeductableAmount'] id PS C:\Users\uswarv41\Box Sync\Documents\GitHub\appd-selenium-scripts>
- Mark as New
- Bookmark Message
- Subscribe to Message
- Mute Message
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi @Arthur.Valijan,
Thank you for your post to the community!
From the given information, it is difficult to give the exact approach you need. As the error message says, the element is no longer accessible but this happens for different reasons, due to the website structure, network latency, the server environment, the client environment, etc. I found these pages and maybe they will help you to understand the issue and take a next step.
https://www.softwaretestingmaterial.com/stale-element-reference-exception-selenium-webdriver/
https://stackoverflow.com/questions/12967541/how-to-avoid-staleelementreferenceexception-in-selenium
As you mention it rather happens randomly, I would like to suggest to execute the script many times locally and take some statistics. It may reveal facts for the error, like on which element it happens more, in which time range happens more, etc.
We also have documentation for waiting for DOM Elements.
https://docs.appdynamics.com/appd/22.x/latest/en/end-user-monitoring/synthetic-monitoring/browser-sy...
Hope this answer helps.
Best regards,
Kenji
