1

I am having no luck selecting an option from a <select> using selenium. I have referenced https://sqa.stackexchange.com/questions/1355/what-is-the-correct-way-to-select-an-option-using-seleniums-python-webdriver

The html code is as follows:

<select name="Dropdownlistrequests" onchange="javascript:setTimeout(&#39;__doPostBack(\&#39;Dropdownlistrequests\&#39;,\&#39;\&#39;)&#39;, 0)" id="Dropdownlistrequests" style="height:51px;width:174px;Z-INDEX: 104; LEFT: 280px; POSITION: absolute; TOP: 72px">
    <option selected="selected" value="1">Previous Days</option>
    <option value="2">Previous Month</option>
    <option value="3">Last 12 Hours</option>
    <option value="4">Demand Poll</option>
    <option value="6">Custom</option>
</select>

I have tried

requests = driver.find_element_by_id("Dropdownlistrequests")
requests.click()
for option in requests.find_elements_by_tag_name('option'):
    if option.text == "Custom":
        option.click()
        break

And

requests = Select(driver.find_element_by_id("Dropdownlistrequests"))
requests.select_by_value("6")

And

b.find_element_by_xpath("//select[@id='Dropdownlistrequests']/option[text()='Custom']").click()

Instead of selecting the appropriate option the browser does nothing and moves on to the next bit of code. Could it have something to do the the javascript that is triggered by the onchange?

To provide some more context: I am running windows 7 enterprise and using selenium with marionette and Firefox developer edition 49.0a2

Update: This only sems to happen when usoing Marionette in python. I tried this same code in Java with and without Marionette and it worked

Community
  • 1
  • 1
pmaurais
  • 734
  • 2
  • 9
  • 30
  • 1
    can you please try using option.get_attribute("innerText") instead to acquire "Custom"? and can you please introduce a bit "wait" after you click on "request" so that the click will not be too fast to be missed. – Yu Zhang Jul 06 '16 at 21:36
  • @YuZhang the current loop will reach the `option.click()` statement, if I change the conditional to `if option.get_attribute("innerText") == "Custom":` the `click()` statement is never reached. Waiting after the click also does nothing. I'm baffled. – pmaurais Jul 06 '16 at 22:16
  • 1
    Try: `requests=Select(driver.find_element_by_id("Dropdownlistrequests")) requests.select_by_visible_text("Custom")` – ishikun Jul 06 '16 at 22:55
  • @ishikun I've tried that too with no success – pmaurais Jul 06 '16 at 22:58
  • 1
    I'm wondering if it's the Marionette driver. Do you have a requirement to use the FF dev version? You might try the old driver and see if it works. You may have found a bug. Your code looks fine to me. – JeffC Jul 07 '16 at 00:09
  • @JeffC when I started this project FF 47 was not supported by Marionette with python (official instructions recommend FF Dev : https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver) and FF 47 was also not supported by the standard selenium web driver (wires I think). Let me try it without marionette or with an older version. – pmaurais Jul 07 '16 at 00:41
  • @JeffC No luck whenever I'm using Marionette. See my update above. – pmaurais Jul 07 '16 at 17:13
  • Yeah... that's a bummer. You should enter a bug for this issue if one doesn't exist already. – JeffC Jul 07 '16 at 18:33
  • 1
    @JeffC I submitted on here https://bugzilla.mozilla.org/show_bug.cgi?id=1285241 – pmaurais Jul 07 '16 at 18:45

1 Answers1

0

If non of the work in your case you should try .execute_script() as below :-

select = driver.find_element_by_id("Dropdownlistrequests")

driver.execute_script("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }",select, "Custom")

Edited :- Above code only select the provided option from select box. If you want trigger the onchange event as well when option selected, try as below :-

select = driver.find_element_by_id("Dropdownlistrequests")

driver.execute_script("showDropdown = function (element) {var event; event = document.createEvent('MouseEvents'); event.initMouseEvent('mousedown', true, true, window); element.dispatchEvent(event); }; showDropdown(arguments[0]);",select)
# now you dropdown will be open


driver.find_element_by_xpath("//select[@id='Dropdownlistrequests']/option[text()='Custom']").click()
#this will click the option which text is custom and onchange event will be triggered.

Hope it will work for you..:)

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • I tired the above code and it changed the value of the ` – pmaurais Jul 07 '16 at 14:16
  • You mean you like as simly open selectbox show all options and then click at option..Right?? – Saurabh Gaur Jul 07 '16 at 14:43
  • Your code just changes the label in the select box. I just want to click the 6th option I do not need all the options to be shown. – pmaurais Jul 07 '16 at 14:46
  • @pmaurais please try with my updated answer. it will trigger the onchange event...:) – Saurabh Gaur Jul 08 '16 at 05:39