2

I have the following select:

<div class="col-xs-12 col-md-6">
<label class="ph_label pointer city">
<span class="l citylbl">City</span>
<select class="cities custom-dropdown" data-validation-error-msg="You  must select a city" required="" name="city" disabled="disabled" data-validation="required">
<option value="">City</option>
<option value="Shanghai">Shanghai</option>
<option value="Beijing">Beijing</option>
<ul class="cities custom-dropdown" data-target-selector="select.cities" style="width: 293px;">
</label>
</div>

I'm trying:

mySelect = Select(self.driver.find_element_by_name("city"))
mySelect.select_by_value("Shanghai")

When I see the test run, selenium goes through this element as if everything was OK, but in fact the city was not selected. Am I missing something?

  • Strange indeed. If your option value and visible text are always the same, try select_by_visible_text() - IIRC it is faster. Is it possible that city is not found, select raises exception, and you silence it somewhere? – Peter M. - stands for Monica Jan 15 '16 at 16:00
  • I've tried putting select_by_visible_text() but nothing interact with this element. – Rafael C. Jan 15 '16 at 16:15
  • You might try selecting by index of the dropdown if it is consistent – Swagin9 Jan 15 '16 at 16:20
  • Did you tried to step through your code with a debugger? Put line #import pdb;pdb.set_trace() before mySelect =. BTW according to PEP8, it should be my_select :-) – Peter M. - stands for Monica Jan 15 '16 at 16:59
  • What UI framework are the developer using? The select is hidden, you need to interact with UI not the hidden select. – Niels van Reijmersdal Feb 19 '17 at 13:56
1

The problem is in the <select> html: disabled="disabled"

This disables that particular select box. Selenium can only interact with the web page as if it were a user, so it cannot set the <select> directly.

You need to figure out what actually sets the value of the <select>, then use Selenium to drive that.

  • This is the answers, most UI frameworks work like this. We are using Semantic UI and this framework also hides the select, you need to interact with the HTML/Javascript dropdown replacing the browser select tag. – Niels van Reijmersdal Feb 19 '17 at 13:55
0

If You cannot alter the html directly You can insert some script code to re-enable the select field:

driver.execute_script("$('select.cities').removeAttr('disabled')")
0

I guess this is using AngularJS and select2. If yes, there are 2 ways 1. Click on the dropdown, then search the value you need

mySelect.Click();

selectSearchBox.Sendkey(searchBox,"Shanghai");

  1. Using JS to enable/display the dropdown as @Joern Boegeholz commen

driver.execute_script("$('select.cities').removeAttr('disabled')")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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