3

A text has to be entered in a Textbox, a list auto-extends, and I need to select the first item. But it fails due to the exception; OpenQA.Selenium.NoSuchElementException. I tried using wait.Until(), but facing the same error.

Screenshot

try
{
     IWebElement cityList = driver.FindElement(By.XPath("value"));
     MouseClick(driver, cityList);
}
catch (OpenQA.Selenium.NoSuchElementException ex)
{
     IWebElement cityList = driver.FindElement(By.XPath("value"));
     MouseClick(driver, cityList);
}

Edit

HTML code:

<input name="ctl00$cphmain$txtCity" type="text" maxlength="50" id="ctl00_cphmain_txtCity" class="mandsearchtxtbox" onkeypress="javascript:return ValidateInputAlphabeticValuesOnly(event);" onblur="javascript:return checkItemMsg(this)" style="width:180px;" autocomplete="off">
<div class="AutoExtenderHighlight">AMANDOLUWA</div>

Code with wait.Until()

WebDriverWait wait1 = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
    wait.Until<IWebElement>((d) =>
        {
            try
            {
                return d.FindElement(By.XPath("//*[@id='citydiv']/div"));
                MouseClick(driver, driver.FindElement(By.XPath("//*[@id='citydiv']/div")));
            }
            catch (OpenQA.Selenium.NoSuchElementException ex)
            {
                return null;
                MouseClick(driver, driver.FindElement(By.XPath("//*[@id='citydiv']/div")));
            }
        });

Edit 2

HTML code for WebDriverException(Button)

HTML code

2
  • Please add the code you have tried as well as the relevant html. Commented May 25, 2017 at 12:08
  • Added the html and tried code. I am not sure if wait.until() code is correct or not. Commented May 25, 2017 at 12:21

1 Answer 1

4

According to the html you posted the id is ctl00_cphmain_txtCity, not citydiv.

Your wait.Until implementation will return IWebElement or null, it will never reach to the MouseClick method. It will also check if the element exists, not visible.

You can use the built in expected conditions class

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//*[@id='ctl00_cphmain_txtCity']/div")));
element.Click();

If you want your own implementation you can do something like

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement option = wait.Until<IWebElement>((d) =>
{
    try
    {
        IWebElement element = d.FindElement(By.XPath("//*[@id='ctl00_cphmain_txtCity']/div"));
        if (element.Displayed)
        {
            return element;
        }
    }
    catch (NoSuchElementException ) { }
    catch (StaleElementReferenceException) { }

    return null;
});

option.Click();

Although I commander you use the built in functionality.

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

13 Comments

Also, ctl00_cphmain_txtCity is the id for the Textbox, whereas citydiv is the id for the auto-extender list. So, the latter id is used.
Another doubt regarding the wait. What if timeout exception is thrown after 10seconds. It won't enter the try block if it fails due to timeout.
@testingqwerty A TimeoutException will be thrown only on the wait.Until, witch is outside the try catch block anyway. The FindElement method will throw only NoSuchElementException.
I am talking about the exception been thrown on 'wait.Until' only
@testingqwerty If you want to catch TimeoutException you can put it inside try catch block as well IWebElement option; try { option = wait.Until<IWebElement>((d) => {...}); } catch (TimeoutException ex){ }
|

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.