0

I'm attempting to get a list of elements in a dropdown list on a page wihtout expanding the list first.

I've got a piece of html, where a dropdown list is created like this:

<est-select id="egenkapitalType0" name="egenkapitalType0" value="Velg type egenkapital…" <div class="select">
   <input type="text" class="select-trigger id="egenkapitalType0-input" placeholder="Velg type egenkapital…">
      <ul role="listbox" id="egenkapitalType0-liste" class="select-liste" aria-hidden="false" ng-transclude="">
         <li id="egenkapitalType0-0" role="option" class="select-valg data-verdi="BANKINNSKUDD_BSU" value="BANKINNSKUDD_BSU">Bankinnskudd/BSU</li>
         <li id="egenkapitalType0-1" role="option" class="select-valg data-verdi="AKSJER" value="AKSJER">Aksjer</li>
      </ul>

(I've removed lots of irrelevant html code, so this sample might lack some closing etc.)

Is this possible? Either the displayed text or the values? Or is the access to the elements dependant on them being visible and accessible on the page?

Sers
  • 12,047
  • 2
  • 12
  • 31
  • Thanks, but no. The result is a "" using that method too. –  Nov 06 '19 at 12:51
  • I take that back. I was able to get it to work using .getAttribute("innerText") !! –  Nov 06 '19 at 13:09

2 Answers2

1

To get the text using getText() the options need to be visible. You can use getAttribute("innerHTML") or getAttribute("textContent") on hidden elements.

Guy
  • 46,488
  • 10
  • 44
  • 88
0

You can access elements and it;s not dependent on visibility.

List<WebElement> elms = driver.findElements(By.xpath("//ul[@role='listbox']//li");    

Now you have list of web elements and you can, get text, for example.

List<String> textFromElements = elms.stream().map(el -> el.getText()).collect(Collectors.toList());    
  • I just get empty strings. But if I click the button first, it works. But the point is kind of wether or not its possible wihtout doing that. –  Nov 06 '19 at 12:38
  • You should inspect dropdown before click, i think options are not present in DOM if you don't click on it. – Alex Kvasko Nov 06 '19 at 12:42
  • When I copy out the html block from the developer console in chrome, the role="option" attribute is included. Also, if it's not there before clicking, shouldn't it return a "not found" or NPE? –  Nov 06 '19 at 12:46