0

Tried searching but did not find any working solutions for this. I have a drop down menu as follows where I'd like to select multiple options at once:

<select name="Area" multiple="" size="5" class="sel0"
onchange="opbygQvar('Area',dummyArray,false,true,false)">
<option value="">(blankstil)
</option><option value="1">1 A
</option><option value="2">2 B
</option><option value="3">3 C
</option><option value="4">4 D
</option><option value="5">5 E
</option><option value="6">6 F
</option></select>

Trial Code:

driver.find_element_by_xpath("//select[@name='Area']/option[text()='1 A']").click()
driver.find_element_by_xpath("//select[@name='Area']/option[text()='2 B']").click()

only selects one option and then changes selection to a different one instead of keeping multiple options checked.

Any help is highly appreciated - thanks in advance :)

1 Answer 1

2

As on Manual operation, if we have to select Multiple values from Multi options Drop down then We have to Select it by using Control click.

Similarly, You have to Automate it by using Control click for Multiple Values.

Example with reference to your case:

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

element1 = driver.find_element_by_xpath("//select[@name='Area']/option[text()='1 A']")
element2 = driver.find_element_by_xpath("//select[@name='Area']/option[text()='2 B']")

ActionChains(driver).key_down(Keys.CONTROL).click(element1).key_up(Keys.CONTROL).perform()
ActionChains(driver).key_down(Keys.CONTROL).click(element2).key_up(Keys.CONTROL).perform()

All you have to do is, control Key binding to select multiple values. Please Note: You can handle control click by multiple ways. Ref Post: Click Here

Sign up to request clarification or add additional context in comments.

3 Comments

hmm - for some reason it does not work. The problem is that the /option[text()='1 A']" seems to be wrong syntax
Its not syntax issue, You need to share exception details for what it did not work.
you are correct, I made it work :-) mistake was on my end

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.