1

I am having trouble with selecting an option from a drop down list which is made visible by clicking on an input text box. I am using Selenium with c#. The functionality is as follows;

  • open web page
  • click on Destination text box
  • this causes a destinations drop down list to be displayed
  • select an option from this list
  • the option is then written to the Destination text box

The HTML for this list is;

 <div id='dvCountryList'>
    <select class="country-list" id="DestinationPicker" multiple="multiple" name="DestinationPicker">
    <option value="AU">Australia</option>
    <option value="ID">Indonesia</option>
    <option value="FJ">Fiji</option>
    <option value="US">United States of America (includes Hawaii)</option>
    <option value="CN">China</option>
    <option value="XA">Worldwide</option>
    <option value="TH">Thailand</option>
    </select>
    </div>

My test code is a follows;

IWebElement destination1 = driver.FindElement(By.ClassName("select2-search__field"));
destination1.Click();

IWebElement destination2 = driver.FindElement(By.ClassName("country-list"));
SelectElement country = new SelectElement(destination2);
country.SelectByValue("AU");

Running this test yields the following error;

OpenQA.Selenium.ElementNotVisibleException : element not visible: Element is not currently visible and may not be manipulated

I tried using a wait before finding the country-list class but that did not help. I am quite new to Selenium so would appreciate any help / feedback. Thanks.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Diren
  • 11
  • 3
  • Seems to be select list seems to appearing after the click ? Can you inspect the option after it pops up? – Rajagopalan Apr 07 '18 at 09:16
  • Yes, the input box control must be clicked in order for the list to be displayed. I'm not sure what you mean by "inspect the option"? – Diren Apr 07 '18 at 23:55
  • this basically happens because there're more than 1 element with the same class or id and you picked the invisible element. check out this https://stackoverflow.com/questions/10641535/how-i-can-avoid-element-is-not-currently-visible-and-so-may-not-be-interacted-w – nam vo Aug 09 '18 at 10:27

1 Answers1

0

To identify the webelement destination2 you have used the Locator Strategy as driver.FindElement(By.ClassName("country-list")); which may not identify the element uniquely. To identify the element you can use the following code block :

IWebElement destination2 = driver.FindElement(By.XPath("//select[@class='country-list' and @id='DestinationPicker']"));
SelectElement country = new SelectElement(destination2);
country.SelectByValue("AU");
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Using this yields the following error message - OpenQA.Selenium.NoSuchElementException : no such element: Unable to locate element: {"method":"xpath","selector":"//select[@class='country-list' and @id='DestinationPicker']"} – Diren Apr 07 '18 at 23:54
  • @Diren did you perform the click inside the text field before you execute DebanjanB's code? – Rajagopalan Apr 08 '18 at 00:55
  • Yes, the click is performed in the first 2 lines of my code above. This ran before the code from DebanjanB – Diren Apr 08 '18 at 02:10
  • @Diren, when you comment me, please put the @ symbol and write my name so that I will be notified . Try this code `IWebElement destination2 = driver.FindElement(By.XPath("//select[@id='DestinationPicker']"));` – Rajagopalan Apr 08 '18 at 09:40
  • @Rajagopalan, sorry I'll include this in the future, thanks. I have tried your suggestion, but I still receive the original error message - OpenQA.Selenium.ElementNotVisibleException : element not visible: Element is not currently visible and may not be manipulated – Diren Apr 08 '18 at 22:19
  • @Diren , Okay element is located now but it's waiting for visibility . Actually I work in Selenium Ruby binding and Java binding. If you have been using ruby binding I can have a look at your system ,but now I can't, please try looking closely you may find something . – Rajagopalan Apr 09 '18 at 05:20