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

Guy
  • 46,488
  • 10
  • 44
  • 88
testing qwerty
  • 105
  • 1
  • 1
  • 12

1 Answers1

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.

Guy
  • 46,488
  • 10
  • 44
  • 88
  • 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. – testing qwerty May 25 '17 at 13:14
  • 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. – testing qwerty May 26 '17 at 06:01
  • @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`. – Guy May 26 '17 at 06:46
  • I am talking about the exception been thrown on 'wait.Until' only – testing qwerty May 26 '17 at 06:53
  • @testingqwerty If you want to catch `TimeoutException` you can put it inside `try catch` block as well `IWebElement option; try { option = wait.Until((d) => {...}); } catch (TimeoutException ex){ }` – Guy May 26 '17 at 07:00
  • As per the code mentioned in the Answer, I am facing `WebDriverTimeoutException`, also on putting the code in `try-catch` block, it is failing. – testing qwerty May 26 '17 at 10:41
  • @testingqwerty Can you share your current code? And can you share the site link? – Guy May 28 '17 at 04:22
  • The link is hosted on local server. – testing qwerty May 29 '17 at 06:50
  • @testingqwerty The `try catch` block will prevent the test to fail on this exception, but it still means the element wasn't located. You can't click on it. Check if its inside ` – Guy May 29 '17 at 08:13
  • The element is not in ` – testing qwerty May 29 '17 at 08:52
  • @testingqwerty Is this html is for the dropdown choices or for something else? – Guy May 29 '17 at 08:56
  • Drop down is working. But for another button click, I was facing same error. For that I got `WebDriverTimeoutException`. HTML code is for that element. – testing qwerty May 29 '17 at 09:06
  • @testingqwerty In that case you should post new question with your code and html (as text, not picture). The comments are not meant to answer new questions. – Guy May 29 '17 at 09:08