Reputation: 5078
This is a issue I did not expect to run into. I am writing a selenium Webdriver test using JUnit 4 in eclipse on Ubuntu 11. I had been using Selenium IDE combined with firebug and firepath to make sure that the xpaths I was specifying in my JUnit test were correct. What I've run into is a problem where the selenium IDE command,
command 'click' at target '//span[contains(text(),'MyTarget')]/PATHTOTARGET'
passes every time. However when I use the following in webdriver it always fails
driver.findElement(By.xpath("//span[contains(text(),'MyTarget')]/PATHTOTARGET")).click();
I have been using Selenium IDE and Selenium Webdriver for a couple months now so have written my share of click commands and never run into this. Anyone have an idea what could be causing this?
The xpath is NOT changing upon refresh it is valid each time. I have also tried waiting for everything on the page to load without luck.
edit1: This was an issue caused by the way our application's extJS context menus worked. If you selected one item from the context menu our application would do some work that caused the context menu to go out of selenium's focus. Adding a refresh followed by an extended wait before selecting a new menu item worked the best.
Upvotes: 3
Views: 2344
Reputation: 31
The post is old, but I came across it while searching for the answer for the same problem. So my solution might help someone. It turned out that I had a frame in the page and the required elements were inside that frame. More than that, when the page was loaded, it did not contain any elements, only link to the frame source. When I added the line driver.switchTo.frame(0); everything worked.
Upvotes: 3
Reputation: 38434
Since the exception you're getting is a StaleElementReferenceException
, it seems that you're still too quick. Explanation:
You need to wait for the previous action to finish, preferably to wait for some result of the action. Is there something new on the screen? Wait for it!
Upvotes: 4