0

I'm using C# in Visual Studio Community 2017, Selenium.Firefox.Webdriver v0.16.1, Selenium.WebDriver v3.4.0.

I am extracting text from a series of drop downs and in doing so need to add 1 to an int representing the index as selecting one dropdown populates the next one with different content depending on the model selected.

I have already extracted all values and stored in a list removing the first value of 'please Select'.

I've used:

selectModel.SelectByIndex(ModelIndex+1);

But intermittently get the OpenQA.Selenium.NoSuchElementException: 'Cannot locate option with index: 1' on the second dropdown and always on the third.

If I replace with:

selectModel.SelectByIndex(1);

The code I've written works fine, but I'm wanting to increment through the index until it is exhausted.

I am incrementing and decrementing the index value now but I don't think it is the best way of doing things:

var modelDropDown = driver.FindElement(By.Name("Model"));
var selectModel = new SelectElement(modelDropDown);
ModelIndex++;
selectModel.SelectByIndex(ModelIndex);
ModelIndex--;

HTML example of the second dropdown:

<select name="Model" id="Model-select" class="form-control asset-option-select ng-pristine ng-not-empty ng-valid ng-valid-required ng-touched" required="" ng-options="option.label for option in dropdown.options track by option.value" ng-model="dropdown.selected" ng-change="select_option(dropdown)" ng-disabled="model.is_locked || dropdown.options.length === 0">
<option value="" selected="selected" disabled="disabled" class="" hidden="">Please Select</option>
<option label="Cherokee" value="Cherokee">Cherokee</option>
<option label="Grand Cherokee" value="Grand Cherokee">Grand Cherokee</option>
<option label="Renegade" value="Renegade">Renegade</option>
<option label="Wrangler" value="Wrangler">Wrangler</option>
<option label="Pre-current Cherokee" value="Pre-current Cherokee">Pre-current Cherokee</option>
</select>

Maybe I'm going about the whole thing the wrong way but that's why I'm asking here, I'm mainly wanting to know why I am getting the error with:

SelectByIndex(ModelIndex+1)
Steaton
  • 1
  • 1

1 Answers1

0

Now I feel embarrassed, it's as simple as let the dropdown contents load before trying to access it.

That's why there was no index of 1 as the dropdown only contains a placeholder until the contents loads.

Thread.Sleep(500);

is enough to demonstrate this, ideally, it should wait for the contents to load before getting the content rather than just using a Thread.Sleep but for now it demonstrates why this problem was occurring.

Steaton
  • 1
  • 1
  • 1
    You shouldn't do this, since the option maynot load in 500ms, instead you should pool and have some deterministic mechanism like explicit wait. You can check answer here and do something similar for c# https://stackoverflow.com/questions/6568081/selenium-how-to-wait-for-options-in-a-select-to-be-populated – nilesh Jun 16 '17 at 23:37
  • Yes, I agree it's not the best way I ideally need it to wait for the contents of the drop down to load. Not sure how to do this as the contents can change so can't wait for a specific item to load. – Steaton Jun 20 '17 at 15:25
  • One way would be to create a library that takes a generic option, and pass the option you want to wait from the test – nilesh Jun 21 '17 at 14:33