I am using Selenium via Java.
I am running into a stale element exception and I can't seem to identify the issue.
I am interacting with a carousel (see image below). I have placed all the vehicles, in the carousel, into a list and I am trying to get the text back from each web element in the list. The list contains eight elements. When my code interacts with the last page of the carousel (in this case the sixth element in the list) a stale element exception gets thrown.
Here is the code where I placed all the vehicles in the carousel in a list:
List<WebElement> vehiclesInCarousel = driver.findElements(By.cssSelector("div[data-qaid='cntnr-frntlnlstng-carousel'] ul[class='slider-list'] li[class='slider-slide']"));
-
Did you tried to identify the element in the last page? (ie) Is that accessible on finding as a separate webelement instead of list?Ajmal– Ajmal2016年06月14日 05:25:18 +00:00Commented Jun 14, 2016 at 5:25
-
Can you show the sample relevant html?Rao– Rao2016年06月19日 16:59:56 +00:00Commented Jun 19, 2016 at 16:59
-
Is there anything happening (in your code and/or on the page) between finding the elements and getting their text?FDM– FDM2016年07月13日 13:18:17 +00:00Commented Jul 13, 2016 at 13:18
2 Answers 2
Two reasons for Stale element
An element that is found on a web page referenced as a WebElement in WebDriver then the DOM changes (probably due to JavaScript functions) that WebElement goes stale.
The element has been deleted entirely.
When you try to interact with the staled WebElement[any above case], the StaleElementException is thrown.
Solutions to resolve them:
- Storing locators to your elements instead of references
DON'T
driver = webdriver.Firefox(); driver.get("http://www.github.com"); search_input = driver.find_element_by_name('q'); search_input.send_keys('hello world\n'); // Page contents refresh after typing in search results. time.sleep(5); search_input.send_keys('hello frank\n'); // StaleElementReferenceException
Do
driver = webdriver.Firefox(); driver.get("http://www.github.com"); search_input = lambda: driver.find_element_by_name('q'); search_input().send_keys('hello world\n'); time.sleep(5); search_input().send_keys('hello frank\n') // no stale element exception
- Leverage hooks in the JS libraries used
# Using Jquery queue to get animation queue length. animationQueueIs = """ return $.queue( $("#%s")[0], "fx").length; """ % element_id wait_until(lambda: self.driver.execute_script(animationQueueIs)==0)
- Moving your actions into JavaScript injection
self.driver.execute_script("$(\"li:contains('Narendra')\").click()");
- Proactively wait for the element to go stale
# Wait till the element goes stale, this means the list has updated wait_until(lambda: is_element_stale(old_link_reference))
This solution, which worked for me, I have mentioned here if you have any additional scenario, which worked for you then comment below or contact me so that we can add into solution list
-
Correct reason, bad solutions.FDM– FDM2016年07月13日 13:16:24 +00:00Commented Jul 13, 2016 at 13:16
-
@FDM - Modified my solutions. Response accordinglyNarendra Chandratre– Narendra Chandratre2016年07月13日 13:22:09 +00:00Commented Jul 13, 2016 at 13:22
Consider using Page Objects:
@FindAll({@FindBy(css = "LocatorHere")})
private List<WebElement> listBoxItems;
Constructor to initialise:
public ClassConstructor(WebDriver driver) {
PageFactory.initElements(driver, this);
this.driver = driver;
}
Generate your WebElement
List:
List<WebElement> selections = listBoxItems;