Reputation: 67
tested HTML:
<select>
<option value="html">html</option>
<option value="css">css</option>
<option value="JavaScript">JavaScript</option>
<option value="php">php</option>
</select>
there's no method like isMultiple()
under Class selenium.webdriver.support.select.Select(webelement)
, also no select_all()
method
when I select these 4 options one by one
Select(lang).select_by_visible_text("html")
Select(lang).select_by_visible_text("css")
Select(lang).select_by_visible_text("JavaScript")
Select(lang).select_by_visible_text("php")
and then try to get all selected options
Select(lang).all_selected_options
I can only get the last option 'php', which means when I select one option, the other one is deselected automatically. What's the meaning of all_selected_options
, options
is useful enough. And I can't deselect any option as there's only one selected, an error reported:
NotImplementedError: You may only deselect options of a multi-select
Upvotes: 0
Views: 2484
Reputation: 193338
As per the HTML you have shared:
<select>
<option value="html">html</option>
<option value="css">css</option>
<option value="JavaScript">JavaScript</option>
<option value="php">php</option>
</select>
The <select>
tag doesn't have the attribute multiple
. So possibly it isn't a multi-selectdrop-down-menu.
To extract the text from the <option>
tags you can use you can use either of the following Locator Strategies:
Using tag_name
:
select_technology = Select(driver.find_element_by_tag_name('select'))
for option in select_technology.options:
print(option.text)
Using xpath
:
select_technology = Select(driver.find_element_by_xpath(//select))
for option in select_technology.options:
print(option.text)
Note : You have to add the following imports:
from selenium import webdriver
from selenium.webdriver.support.select import Select
Upvotes: 0
Reputation: 820
If you want to select multiple options using selenium in python, you can always use ActionChains
to chain series of action, we need following actions in our case:
CTRL
key Click
on optionCTRL
keyHere is a good example of using ActionChains in python
Make a list of options you want to select in python, loop through list and use xpath
to select option containing the text
and then use ActionChains
to select the option using series of actions as defined above.
# Text of options needed to select
options = ['html','css','php']
# Add path to your chrome drive
browser = webdriver.Chrome(executable_path="EXECUTABLE_PATH_HERE")
# Add url of website
browser.get("WEBSITE_URL_HERE")
for option in options:
# Find option that contains text equal to option
to_select = browser.find_element_by_xpath("//select/option[text()='"+option+"']")
# Use ActionChains
ActionChains(browser).key_down(Keys.CONTROL).click(to_select).key_up(Keys.CONTROL).perform()
ActionChains()
get reference of driver that is browser
in this case.key_down()
press the key that is CONTROL
passed to it.click()
click the passed option that is selected using xpath
.key_up()
release CONTROL
keyI hope this will help you a lot.
Upvotes: 1
Reputation: 50949
This dropdown doesn't support multi selection, such dropdown will have multiple
attribute
<select multiple="">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
There is no is_multiple()
function, but there is a variable is_multiple
. It's created in Select
__init__
by checking the multiple
attribute
def __init__(self, webelement):
self._el = webelement
multi = self._el.get_attribute("multiple")
self.is_multiple = multi and multi != "false"
You can access it using Select
instance
Select(element).is_multiple
To get all the dropdown options regardless if they are selected use option
property, this will return all the options as WebElement
list
options = Select(element).options
for option in options:
print option.text # html, css, ...
Upvotes: 0