How to double click an option in a multi select using Selenium and Python

1.6k views Asked by At

I have a multi select box that has options that you have to double click in order to send it to a 'Selected' field. No matter what I try, I can't seem to get it to work. The select html looks like so:

<select name="name1" id="id1" ondblclick="a lot of stuff">
    <option value='value1'>text1</option>
    <option value='value2'>text2</option>
    <option value='DoubleClickMe'>DoubleClickMe</option>
    <option value='value4'>text4</option>
    <option value='value5'>text5</option>
</select>

I want to double click the 'DoubleClickMe' value to send it over to another field. I've tried:

ret = driver.find_element_by_xpath("//select[@id='id1']/option[text()='DoubleClickMe']")

actionChains.double_click(ret).perform()

Originally, this would act like it was double clicking elsewhere on the page (at least highlight some other text). Now... it seems to be selecting a bunch of options to send over, as if it was clicking a heck of a lot more than twice. Similarly, I tried:

actionChains.click(ret).click(ret).perform()

This one is giving me the same results as the previous one.

While in debug mode (pdb), I tried spamming the following (each of which will select the option, but won't read it as a double click:

ret.click()

ret = driver.find_element_by_xpath("//select[@id='id1']")
select = Select(ret)
select.select_by_visible_text("DoubleClickMe")

select.select_by_value("DoubleClickMe")

I tried sending those commands quick enough as to where, if working properly, it should easily be considered a double click.

Is there something that I'm missing or doing wrong?

I'm using:

Windows 7 64-bit
Selenium 2.44
Python 2.7
IE11
2

There are 2 answers

7
alex On

Just throwing out ideas... havn't tested this though

(edited)

user = self.find_element_by_id("id1")
for option in user.find_elements_by_tag_name("option"):
   if option.text == "Option to be selected":
      option.click()
5
Santoshsarma On

Try with below logic

menu = driver.find_element_by_css_selector("#id1")
option = menu.find_element_by_css_selector("option[value='DoubleClickMe']")

ActionChains(driver).double_click(option).perform()

Update 1

Try with below logic. First move the mouse to that element and try to double click without passing element.

ActionChains(driver).move_to_element(option).double_click().perform()
Or
ActionChains(driver).move_to_element(option).click().click().perform()