I am applying the selenium webdriverwait method to a particular IWebElement to fetch some child elements of this IWebElement when they are available. This is my code...
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IList<IWebElement> elementsA = wait.Until<IList<IWebElement>>((d) =>
{
return driver.FindElements(By.XPath("//table[@class='boxVerde']"));
});
foreach (IWebElement iwe in elementsA)
{
wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IList<IWebElement> elementsB = wait.Until<IList<IWebElement>>((d) =>
{
return iwe.FindElements(By.XPath("tr/td/div/a"));
//trying to fetch the anchor tags from the element
});
}
it keeps giving me an error saying 'element no longer attached to DOM'...I think that the webdriver wait is simply not working. Am I doing anything wrong guys? much thanks in advance
It sounds like you are suffering from a Stale Element.
Every WebElement that Selenium finds is actually a reference to a element inside the DOM
Sometimes the JavaScript used on a site will destroy and recreate an element, when this happens your existing reference becomes stale and you will see a StaleElementReferenceException (At least that's what it's called in Java land, You should be seeing something very similar).
So if you ever see a StaleElementReferenceException or a message stating that the "element no longer attached to DOM", it means that the reference to an element in the DOM that you used to have is no longer valid because the element you had a reference to has been destroyed.
This isn't always obvious visually because the original element may have been destroyed and then an identical one recreated, it is however a new element so you need to create a new reference to interact with it.
The way to create a new reference is to find the element again.
It's not totally clear from your questions, but I am assuming you are getting the issue when iterating through the list of anchor elements after finding the table. If this is the case it suggests that the table has been destroyed and recreated between the point you found the table element and then started to iterate through the anchor elements inside the table.
If this is the case you will need to find the table again, a useful trick is to wait for the table to become stale before you try and find it again. This way you can be reasonably confident that any JavaScript on the page that is destroying the table and then re-creating it has finished processing. The .NET ExpectedConditions class is not as mature as the Java ExpectedConditions class. I would suggest having a look at some of the Java ones and porting them across to .NET (It should be fairly trivial, the structure is pretty similar), of particular interest is the Java one that waits for an element to become stale.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With