0

I have a select that looks like this:

<select data-origvalue="" multiple="multiple" name="project_9999" id="project_9999" style="display: none;">
<option value="1">Option1</option>
<option value="2">Option2</option>
</select>

I am trying to view the options like this...

select = Select(driver.find_element_by_id('project_9999'))
print([o.text for o in select.options])

...which results in:

['','']

Why is the text of each option not appearing in the list? How would I select one of the options without the options being fully visible?

OverflowingTheGlass
  • 2,324
  • 1
  • 27
  • 75
  • 1
    I don't see an element named `project_template_1511`. – John Gordon Jun 17 '20 at 21:45
  • if you use `select = ...` second time then you remove previous value. Did you use `print(select)` after each `select = ...` ? – furas Jun 17 '20 at 21:47
  • 1
    Also, what do you mean by _... which results in:_ ? All you've done is find an element and initialize a `Select` object from it; there is no code that would print out the options of that Select object. – John Gordon Jun 17 '20 at 21:48
  • better create minimal working code with real url. And then we can run it and test what can be problem. – furas Jun 17 '20 at 21:51
  • very sorry - I pasted the wrong line of code. updated now. – OverflowingTheGlass Jun 18 '20 at 13:09

1 Answers1

0

You need to iterate over the child elements of project_9999, not the parent select element. You can do that like so

options = driver.find_element(By.ID, 'project_9999')
for option in options.find_elements(By.TAG_NAME, 'option'):
    print(option.get_attribute('innerHTML'))

Returns

Option1
Option2

You'll probably want to add in some error handling if the element doesn't exist etc.

Lucan
  • 2,907
  • 2
  • 16
  • 30
  • how would I actually select one of the options using this method? – OverflowingTheGlass Jun 18 '20 at 13:13
  • Because your element is `display: none`, I think you'll have to select it using [Javascript](https://stackoverflow.com/a/29273838/11916773). You can either set the `display` to shown or manually select your option. – Lucan Jun 18 '20 at 14:20