2

I am trying to list the possible options in this Select element.

<select id="sl_coursePage" onchange="document.frm.submit();" name="sl_coursePage">
    <option selected="selected" value="0"></option>
    <option value="1"></option>
    <option value="2"></option>
    <option value="3"></option>
    <option value="4"></option>
    <option value="5"></option>
    <option value="6"></option>
    <option value="7"></option>
    <option value="8"></option>
    <option value="9"></option>
    <option value="10"></option>
</select>

I can find the element itself fine with elem = driver.get_element_by_name('sl_coursePage'), but then when I tried to list the options by using elem.get_elements_by_xpath('.//*') or elem.get_elements_by_tag_name('option'), it gives me empty lists.

So then I tried to use Select from selenium.webdriver.support.ui and get the list by the following code:

elem = driver.get_element_by_name('sl_coursePage')
select = Select(elem)
list = select.options

However, when I try this, it raises this error:

selenium.common.exceptions.UnexpectedTagNameException: Message: Select only works on <select> elements, not on <input>

I don't understand how this is not a Select element when its tags say "select". If anyone can get any of these methods to work or present a functioning alternative method, I would be extremely grateful. Thanks in advance.

2 Answers 2

1

It looks like there is an another element with the same name which is not select and get matched by the "by name" locator. Improve the way you locating the element explicitly asking for select element with the desired name:

elem = driver.find_element_by_css_selector('select[name=sl_coursePage]')

Or, checking the id:

elem = driver.find_element_by_css_selector('select#sl_coursePage')
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much! That did the trick. And thank you for the fantastically quick reply as well!
0

You are trying to fetch the element by name

elem = driver.get_element_by_name('sl_coursePage')

Try to fetch it from id or a css selector instead, something like

elem =driver.findElement(By.id('sl_coursePage'))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.