0

I am trying to click items from a select box using selenium like this

from selenium.webdriver.support.ui import Select
...
county_list = Select(driver.find_element_by_id('cityCode'))
for option in county_list.options:
  print(f"option.text == {option.text}")

However, I am getting only one option from the for loop when there are more than one options. I feel like the problem is that an option is commented out in the html like below

<select id="cityCode">
  <option value="-1">Choice</option>
  <!-- option value="0">All</option-->
  <option value="1">First</option>
  <option value="2">Second</option>
  <option value="3">Third</option>
</select>

Is there any way to get all the child elements using Selenium?

jkang14
  • 53
  • 5
  • Are you sure you are identifying `county_list` correctly? If you do a `driver.find_element_by_id('cityCode').click()` does it work? Also what is the one option you get from the `for` loop? – 0buz Mar 16 '20 at 15:08
  • @0buz it works. I am only getting the first option in my for loop – jkang14 Mar 26 '20 at 16:05

1 Answers1

0

Change your code as below.

from selenium.webdriver.support.ui import Select
...
# you can use either xpath "//select[@id='cityCode']/option"
# or css "select#cityCode option"
options= driver.find_elements_by_xpath("//select[@id='cityCode']/option")
for option in options:
  print(option.text)
supputuri
  • 13,644
  • 2
  • 21
  • 39