0

tested HTML:

<select>
    <option value="html">html</option>
    <option value="css">css</option>
    <option value="JavaScript">JavaScript</option>
    <option value="php">php</option>
</select>
  1. there's no method like isMultiple() under Class selenium.webdriver.support.select.Select(webelement), also no select_all()method

  2. 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
Guy
  • 46,488
  • 10
  • 44
  • 88
elf
  • 57
  • 10
  • This dropdown doesn't seem to support multi select. Can you select more than one option manually? – Guy Dec 12 '19 at 08:34
  • Of course not. then ```all_selected_options``` and ```deselect_``` methods are all no use? – elf Dec 12 '19 at 08:40
  • 1
    Just because this dropdown doesn't support multi select, it doesn't mean all the dropdowns doesn't support it. – Guy Dec 12 '19 at 08:41

3 Answers3

1

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:

  1. Press CTRL key
  2. Click on option
  3. Release CTRL key

Here 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 key

I hope this will help you a lot.

Ghayoor ul Haq
  • 670
  • 6
  • 24
0

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, ...
Guy
  • 46,488
  • 10
  • 44
  • 88
  • there's neither ```is_multiple()``` nor ```is_multiple``` under ```Select``` class. I checked in the selenium doc: https://selenium.dev/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.select.html#module-selenium.webdriver.support.select – elf Dec 12 '19 at 15:16
  • @elf It's not in the docs, but you can see it in the source code. Try to use it (without the `()`). – Guy Dec 12 '19 at 15:28
0

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 .


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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352