Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium WebDriver access a sub element

I have a div with a unique ID. Under that div are a bunch of span elements that have className=foo. There are several span elements with className=foo but they are unique to each div (if that's clear). So my Selenium code first gets the unique div as a web element then tries to take that element and get by class name the span like so

element = sDriver.findElement(By.id("c_"+cID)); 
String sTest = element.findElement(By.className("actions")).getText();

On the second line it throws an exception every time

org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
Command duration or timeout: 22 milliseconds

Do I misunderstand how to get that span from under a unique div?

like image 991
ducati1212 Avatar asked Jan 12 '12 14:01

ducati1212


People also ask

How do you get inner element in Selenium?

The Selenium WebDriver interface has predefined the getText() method, which helps retrieve the text for a specific web element. This method gets the visible, inner text (which is not hidden by CSS) of the web-element.

How do you get children of elements in Selenium?

We can locate child nodes of web elements with Selenium webdriver. First of all we need to identify the parent element with help of any of the locators like id, class, name, xpath or css. Then we have to identify the children with the findElements(By. xpath()) method.

What is xOffset and yOffset in Selenium?

Both xOffset and yOffset represent the relative pixel distance (integer) with which to scroll. For xOffset , positive value means to scroll right, and negative value means to scroll left. For yOffset , positive value means to scroll downward, and negative value means to scroll upward.

How does Selenium handle menu and submenu?

We can select an item from the sub-menu of a menu using mouse over action in Selenium webdriver with the help of the Actions class. We shall create an object of the Actions class and then apply moveToElement to it. This method shall move the mouse to the middle of the menu which displays submenu on mouse over.


2 Answers

Nope you'right accessing the span but the problem is that the Dom has changed since StaleReferenceException is about (see StaleReferenceException )

This may be caused because the page isn't loaded completely when the code starts or changes when the code is executed. You can either try to wait a little longer for the element or catch the StaleReferenceException and try again finding the div and the span.

like image 114
chaosr Avatar answered Nov 11 '22 19:11

chaosr


My solution is not fancy but it works like a Swiss watch (in my situation of course). So my code is calling the parent element in a loop looking for different child elements in it. Nothing got changed at all - just simple querying and the exception began to occur. So! I've added the Thread.Sleep(2000) command before every search of parent element and it solver the problem. Not elegant but works every time with minimum code to debug afterwards.

like image 42
user1922804 Avatar answered Nov 11 '22 18:11

user1922804