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?

2
  • 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? Commented Mar 16, 2020 at 15:08
  • @0buz it works. I am only getting the first option in my for loop Commented Mar 26, 2020 at 16:05

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.