3
votes

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.

2
What is implicit wait set to? By the way, you can always retrieve the full page source from WebDriver if you're stuck. - biziclop
Thanks for the comment! Implicit wait is set to 30 seconds. I will look into how to view the page source from WebDriver. - OrwellHindenberg
It's WebDriver.getPageSource(), should work with FirefoxDriver - biziclop
Getting the page source is a real neat thing to know about, however when I saved the output of WebDriver.getPageSource().toString() to an html file I was able to find the target I had been looking for. So I must be doing something wrong. Will look into my wait for page to load function again. - OrwellHindenberg
Does it fail with NoSuchElementException or something other? - Petr Janeček

2 Answers

4
votes

Since the exception you're getting is a StaleElementReferenceException, it seems that you're still too quick. Explanation:

  1. The element is on the page you're working with.
  2. You click something, the page starts reloading.
  3. But before it reloads, WebDriver has already found your faulty element (it didn't wait for the previous action to finish for some reason).
  4. Then the page finally reloads and WebDriver tries to click the previously found element ... which now doesn't exist, because it was on the previous page (even though it's there now, too).

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!

3
votes

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.