2

I am using Selenium WebDriver and its was working all fine and today i am getting either timeout if i use the below code or getting the error Unable to find element with id == //*[@id='ctl00_ContentPlaceHolder1_AddControl1_txtName']

i try to use this:

    public IWebElement GetElementId(string id)
    {
        //return Driver.FindElement(By.Id(id));
        Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(TimeOut));
        return Driver.FindElement(By.Id(id));
    }

and tried this:

public IWebElement GetElementId(string id)
{
    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
    IWebElement category = wait.Until<IWebElement>((d) =>
    {
        return d.FindElement(By.Id(el_id));
    });
}

I am still couldn't figured how to avoid time-out or element not found error

any help?

2
  • maybe it timed out or the element can't be found? Commented Aug 31, 2012 at 15:51
  • ID Changed? Try another selector? Can we see the HTML around the control? Happen in all drivers or limited to a certain driver? Commented Aug 31, 2012 at 21:51

2 Answers 2

5

Try using the FluentWait class:

public WebElement fluentWait( final By locator ) {
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
        .withTimeout(30, TimeUnit.SECONDS)
        .pollingEvery(5, TimeUnit.SECONDS)
   .ignoring(NoSuchElementException.class, StaleElementReferenceException.class);

    // use a "custom" ExpectedCondition
    WebElement foo = wait.until( new Function<WebDriver, WebElement>() {
        public WebElement apply( WebDriver driver ) {
            return driver.findElement( locator );
        }
    });
    // usually use one of the built-in ExpectedCondtions
    // WebElement foo = wait.until(
    //     ExpectedConditions.visibilityOfElementLocated( locator );
    return  foo;
};

you can read about fluent wait here

Or if not check thoroughly if you found locator properly.

Hope this helps you)

Sign up to request clarification or add additional context in comments.

Comments

1

You are using xpath but in the findElement you are using By.Id change it as

By.xpath("//*[@id='ctl00_ContentPlaceHolder1_AddeCardControl1_txtName']")

                        OR

By.id("ctl00_ContentPlaceHolder1_AddeCardControl1_txtName")

If it still shows timeout error then try by specifying the element name too in the xpath like

//div[@id='element_id']

because specifying like this

//*[@id='ctl00_ContentPlaceHolder1_AddeCardControl1_txtName']

may took time by searching all the elements id attribute so if you specify the particular element then the searching time will be minimized.

If it doesn't works then check whether that your xpath is correct or not.

2 Comments

yea it was typo but i am passing the correct id.. let me ask you... What is the fastest Xpath or ID ? which one is more preferable?
Based on my experience xpath is slower than using Id see this one for more details stackoverflow.com/questions/5313847/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.