How to use getSupportUrl method of org.openqa.selenium.StaleElementReferenceException class

Best Selenium code snippet using org.openqa.selenium.StaleElementReferenceException.getSupportUrl

StaleElementReferenceException org.openqa.selenium.StaleElementReferenceException

The WebDriver error - stale element reference error, happens when the referenced web element isn't attached to the DOM.

Dom elements in the WebDriver are identified by a unique reference called a web element. The web element reference is a globally unique identifier which is utilised in executing commands to target particular elements like getting the property of an element etc.

In JavaScript, a stale element in the DOM has lost its connection to the document after it was deleted from the document or the document was changed. For example, staleness can occur when the web element reference from the document it is retrieved from is navigated away.

Examples

Document navigation

Upon navigating away from the page, all web element references to the previous page will be discarded. A stale element reference error occurs when subsequent interaction with a discarded web element fails because it is no longer in the document:

copy
1import urllib 2 3from selenium import webdriver 4from selenium.common import exceptions 5 6def inline(doc): 7 return "data:text/html;charset=utf-8,{}".format(urllib.quote(doc)) 8 9session = webdriver.Firefox() 10session.get(inline("<strong>foo</strong>")) 11foo = session.find_element_by_css_selector("strong") 12 13session.get(inline("<i>bar</i>")) 14try: 15 foo.tag_name 16except exceptions.StaleElementReferenceException as e: 17 print(e) 18

Copy to Clipboard

Output:

copy
1 StaleElementReferenceException: The element reference of e75a1764-ff73-40fa-93c1-08cb90394b65 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

Node deletion

A document node's web element reference will be invalidated when the node is deleted from the DOM. Any subsequent reference to the web element will also throw the same error:

copy
1import urllib 2 3from selenium import webdriver 4from selenium.common import exceptions 5 6def inline(doc): 7 return "data:text/html;charset=utf-8,{}".format(urllib.quote(doc)) 8 9session = webdriver.Firefox() 10session.get(inline("<button>foo</button>")) 11button = session.find_element_by_css_selector("button") 12session.execute_script(""" 13 let [button] = arguments; 14 button.remove(); 15 """, script_args=(button,)) 16 17try: 18 button.click() 19except exceptions.StaleElementReferenceException as e: 20 print(e)

Output:

copy
1 StaleElementReferenceException: The element reference of e75a1764-ff73-40fa-93c1-08cb90394b65 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

Solutions:

  • Check element presence in DOM
  • Add wait to give time to load element
  • Check if the element is in frame
  • Check if the page is refershed, common problem with JS frameworks

Code Snippets

Here are code snippets that can help you understand more how developers are using

Source:StaleElementReferenceException.java Github

copy

Full Screen

...12 public StaleElementReferenceException(String message, Throwable cause) {13 super(message, cause);14 }15 16 public String getSupportUrl()17 {18 return "http://seleniumhq.org/exceptions/stale_element_reference.html";19 }20}...

Full Screen

Full Screen

getSupportUrl

Using AI Code Generation

copy

Full Screen

1package com.automationrhapsody.selenium;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.chrome.ChromeDriver;6public class StaleElementExceptionDemo {7 public static void main(String[] args) throws InterruptedException {8 System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");9 WebDriver driver = new ChromeDriver();10 WebElement searchBox = driver.findElement(By.name("q"));11 searchBox.sendKeys("Selenium");12 Thread.sleep(5000);13 driver.navigate().refresh();14 searchBox.sendKeys(" WebDriver");15 driver.quit();16 }17}18 (Session info: chrome=75.0.3770.100)19 (Driver info: chromedriver=75.0.3770.140 (0),platform=Windows NT 10.0.17134 x86_64) (WARNING: The server did not provide any stacktrace information)

Full Screen

Full Screen

getSupportUrl

Using AI Code Generation

copy

Full Screen

1public class StaleElementReferenceException {2 public static void main(String args[]) {3 WebDriver driver = new FirefoxDriver();4 WebElement searchBox = driver.findElement(By.name("q"));5 searchBox.sendKeys("Selenium");6 driver.navigate().refresh();7 searchBox.sendKeys(" WebDriver");8 driver.close();9 }10}11Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: The element reference of <input name="q" value="Selenium" maxlength="2048" title="Search" class="gsfi" type="text" jsaction="paste:puy29d"> 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

Full Screen

Full Screen

getSupportUrl

Using AI Code Generation

copy

Full Screen

1package org.openqa.selenium;2import org.openqa.selenium.support.ui.ExpectedConditions;3import org.openqa.selenium.support.ui.WebDriverWait;4import org.testng.annotations.Test;5public class StaleElementReferenceException extends BaseTest {6 public void testStaleElementReferenceException() {7 driver.findElement(By.name("q")).sendKeys("Selenium");8 driver.findElement(By.name("btnK")).click();9 WebDriverWait wait = new WebDriverWait(driver, 10);10 wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Selenium - Web Browser Automation")));11 driver.findElement(By.linkText("Selenium - Web Browser Automation")).click();12 }13}14package org.openqa.selenium;15import org.openqa.selenium.support.ui.ExpectedConditions;16import org.openqa.selenium.support.ui.WebDriverWait;17import org.testng.annotations.Test;18public class StaleElementReferenceException extends BaseTest {19 public void testStaleElementReferenceException() {20 driver.findElement(By.name("q")).sendKeys("Selenium");21 driver.findElement(By.name("btnK")).click();22 WebDriverWait wait = new WebDriverWait(driver, 10);23 wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Selenium - Web Browser Automation")));24 driver.findElement(By.linkText("Selenium - Web Browser Automation")).click();25 }26}

Full Screen

Full Screen

getSupportUrl

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.StaleElementReferenceException;2public class StaleElementReferenceExceptionDemo {3 public static void main(String[] args) {4 StaleElementReferenceException staleElementReferenceException = new StaleElementReferenceException("message");5 String supportUrl = staleElementReferenceException.getSupportUrl();6 System.out.println("Support Url: " + supportUrl);7 }8}

Full Screen

Full Screen

getSupportUrl

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.StaleElementReferenceException;2public class StaleElementReferenceExceptionExample {3 public static void main(String[] args) {4 StaleElementReferenceException staleElementReferenceException = new StaleElementReferenceException("StaleElementReferenceException");5 String supportUrl = staleElementReferenceException.getSupportUrl();6 System.out.println(supportUrl);7 }8}9package org.openqa.selenium;10import java.util.Objects;11public class StaleElementReferenceException extends WebDriverException {12 public StaleElementReferenceException(String message) {13 super(message);14 }15 public StaleElementReferenceException(String message, Throwable cause) {16 super(message, cause);17 }18 public StaleElementReferenceException(Throwable cause) {19 super(cause);20 }21 public String getSupportUrl() {22 }23 public boolean equals(Object o) {24 if (!(o instanceof StaleElementReferenceException)) {25 return false;26 }27 StaleElementReferenceException that = (StaleElementReferenceException) o;28 return Objects.equals(getMessage(), that.getMessage()) &&29 Objects.equals(getSupportUrl(), that.getSupportUrl());30 }31 public int hashCode() {32 return Objects.hash(getMessage(), getSupportUrl());33 }34}

Full Screen

Full Screen

getSupportUrl

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.StaleElementReferenceException;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.chrome.ChromeDriver;4public class StaleElementReferenceExceptionGetSupportUrl {5 public static void main(String[] args) {6 System.setProperty("webdriver.chrome.driver", "C:\\Users\\Shiva Sai\\Downloads\\chromedriver_win32\\chromedriver.exe");7 WebDriver driver = new ChromeDriver();8 try {9 driver.findElementByName("q").sendKeys("Selenium");10 driver.findElementByName("btnK").click();11 } catch (StaleElementReferenceException e) {

Full Screen

Full Screen

Selenium 4 Tutorial:

LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.

Chapters:

  1. Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.

  2. What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.

  3. Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.

  4. Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.

  5. How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.

  6. Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.

  7. Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.

Selenium 101 certifications:

LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in StaleElementReferenceException

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful