1

I followed what was written here: WebDriver Selenium API: ElementNotFoundErrorException when Element is clearly there !

My code looks like:

    Function<WebDriver, WebElement> presenceOfElementLocated(final By locator) {
    return new Function<WebDriver, WebElement>() {
        public WebElement apply(WebDriver driver) {
            return driver.findElement(locator);
          }
       };
    }

   ....... 

   driver.get(baseUrl);

   WebDriverWait wait = new WebDriverWait(driver, 10);
   wait.until(presenceOfElementLocated(By.className("classname")));
   findByClassAndName(driver, "x-tree3-node-text", "Name1").click();

Problem is, that is does not seem to do anything. It doesn't work and i can't even see slightest trace of waiting for webpage gui. i got the same with implicit wait through timeouts... Anyone could help?

Community
  • 1
  • 1
Arek
  • 1,941
  • 4
  • 22
  • 27

2 Answers2

1

Create function as follows :

public void Wait (string element)          // Wait function to wait for element
        { 
            for (int second = 0; ; second++)
                {
                    if (second >= 60) Assert.Fail("timeout");
                    try
                    {
                        if (IsElementPresent(By.LinkText(element))) break;
                    }
                    catch (Exception)
                    { }
                    Thread.Sleep(1000);
                 }  
        }

and now call this function where you want to wait for element as follows:

string element="<element name>";
        Wait(element);
Servy
  • 202,030
  • 26
  • 332
  • 449
Ramesh
  • 33
  • 2
  • 8
  • In general using explicit sleep's is not a good way so i tried to avoid it. – Arek Nov 04 '11 at 10:28
  • Agreed, I hate having to use the whole explicit sleep. It's ugly and not good practice. However some times, it does the job...until a proper fix can be put in place. If only Selenium Webdriver had easier functions for waiting :> – Patrick Magee Nov 16 '11 at 22:37
1

You have to catch Throwables throwed from ExpectedCondition or your Function (the apply() methods is good place for that) and return null so Wait.until() continues to run - see http://rostislav-matl.blogspot.com/2011/05/moving-to-selenium-2-on-webdriver-part.html for detailed example.

Rostislav Matl
  • 4,294
  • 4
  • 29
  • 53