Using webdriverwait for a specific IWebElement

I apply the selenium webdriverwait method to a specific IWebElement to retrieve some children 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
       });
}

he continues to give me an error saying that "the element is no longer bound to the DOM" ... I think that waiting for the webdriver just doesn't work. Am I doing something wrong? thank you very much in advance

+5
source share
4 answers

It looks like you are suffering from an obsolete item.

Every WebElement that Selenium finds is actually a reference to an element inside the DOM

JavaScript, , , , , StaleElementReferenceException ( , , Java-land, - ),

, - StaleElementReferenceException , " DOM", , DOM, , , , .

, , , , , , .

- .

, , . , , , , .

, , , , . , , JavaScript , , , . NET ExpectedConditions , Java ExpectedConditions. Java .NET( , ), Java, , .

+3

, . . //table[@class='boxVerde']/tr/td/div/a xpath .

+1

, Webdriver , , . java- , 60 , , , -. , . (, javist)

public WebElement fluentWait(final By locator, WebDriver driver) {
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
    .withTimeout(60, TimeUnit.SECONDS)
    .pollingEvery(1, TimeUnit.SECONDS)
    .ignoring(StaleElementReferenceException.class)
    .ignoring(NoSuchElementException.class);
    WebElement foo = wait.until(
    new Function<WebDriver, WebElement>() {
    public WebElement apply(WebDriver driver) {
    return driver.findElement(locator);
    }
         }  );
    return foo;}

, , xpath

       iwe.FindElements(By.XPath(" //a[text()='anchor tag text']"));

By.linkText( " " );

       iwe.FindElements(By.linkText("anchor tag text"));
0

. , .

static void ForElementsArePresent(IWebDriver wd, double waitForSeconds, By by)
        {
            try
            {
                WebDriverWait wait = new WebDriverWait(wd, TimeSpan.FromSeconds(waitForSeconds));
                wait.Until(x => (x.FindElements(by).Count > 0));
            }
            catch (WebDriverTimeoutException)
            {
                Console.WriteLine("Waiting for element " + by + " to disappear timed out after " + waitForSeconds + " seconds.");
                throw;
            }                       
        }
-1

All Articles