简体   繁体   中英

Why Selenium can't find an element that is graphically visible

I'm using Selenium to test an appllication running on a firefox browser, sometimes when i want to click on a button with Selenium, it just don't find it. I find this strange since i can see the element with my eye. I have already tried multiple method that worked for other exemple like :

  1. wait.until(ExpectedConditions.visibilityOfElementLocated(by))
  2. wait.until(ExpectedConditions.presenceOfElementLocated(by)); (i use an xpath in the path variable, and i'm sure he is correct)
  3. Setting a implictlyWait on the driver
  4. Putting Thread.sleep(1000)

Then again, i post here because i don't understand why Selenium could not see an element that is diplayed on the browser. An important information that could maybe help you giving me a proper answer is that the html DOM is dynamically generate by websocket triggered by javascript event.

EDIT 1:

<button type="button" class="btn btn-xs btn-block btn-stroke" id="252_button">
    Delete
</button>

The element i'm trying to access. I use an xpath to do it.

It's not in an iframe element.

TO access an element i did this method that is supposed to find and click on it giving the xpath in parameter :

public void findAndClick(String xpath) {
        By by = By.xpath(xpath);
        //wait.until(ExpectedConditions.presenceOfElementLocated(by));
        wait.until(ExpectedConditions.visibilityOfElementLocated(by));

        driver.findElement(by).click();
    }

EDIT 2:

Here the specific xPath : //div[2]/div/div/div/button I've found it using the Selenium plugins.

EDIT 3 :

The Exception i'm getting is

StaleReferenceElementException: Element not found in cache - perhaps the page has change since it was looked up.

I went to the page that explain this Exception, it says that :

The element has been deleted entirely.
The element is no longer attached to the DOM.

But it can't be, becasue i can see it and click on it manually.

I think i found an explanation of my problem, i read the documention about the StaleReferenceException . More precisely in this part :

The Element is not Attached to the DOM

A common technique used for simulating a tabbed UI in a web app is to prepare DIVs for each tab, but only attach one at a time, storing the rest in variables. In this case, it's entirely possible that your code might have a reference to an element that is no longer attached to the DOM (that is, that has an ancestor which is "document.documentElement"). If WebDriver throws a stale element exception in this case, even though the element still exists, the reference is lost. You should discard the current reference you hold and replace it, possibly by locating the element again once it is attached to the DOM.

I think i'm in the same case because this is exactly when i use a method (getText(), getTag()) on the reference of a webElement that i have this exception even if this element is still graphically visible. As i understand it, i try to access a reference which is no longer attached to the DOM since the DOM have changed.

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