Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I am having no luck selecting an option from a <select> using selenium. I have referenced http://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

share|improve this question
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 6 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 6 at 22:16
1  
Try: requests=Select(driver.find_element_by_id("Dropdownlistreque‌​sts")) requests.select_by_visible_text("Custom") – ishikun Jul 6 at 22:55
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 7 at 0:09
1  
@JeffC I submitted on here bugzilla.mozilla.org/show_bug.cgi?id=1285241 – pmaurais Jul 7 at 18:45

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..:)

share|improve this answer
    
I tired the above code and it changed the value of the <select> but did not actually click the option thus not triggering the necessary JavaScript – pmaurais Jul 7 at 14:16
    
You mean you like as simly open selectbox show all options and then click at option..Right?? – Saurabh Gaur Jul 7 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 7 at 14:46
    
@pmaurais please try with my updated answer. it will trigger the onchange event...:) – Saurabh Gaur Jul 8 at 5:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.