简体   繁体   中英

Selenium WebDriver cannot locate Element that Selenium IDE can locate

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.

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM