title: selenium python dynamic dropdown menu
body: <p>I want to fill form from a website automatically, everything goes well until I need to choose an option from dynamic dropdown menu.</p>
  
  <pre><code>&lt;div id="field_type_group" class="field trigger-option group0" style=""&gt;
    &lt;div class="label"&gt;&lt;label&gt;Input Type&lt;/label&gt;&lt;/div&gt;
    &lt;div class="input"&gt;
      &lt;select&gt;
        &lt;option data-item-id="100" value="text_field_validation_type"&gt;Text input&lt;/option&gt;
        &lt;option data-item-id="200" value="dropdown_field_type"&gt;Dropdown list&lt;/option&gt;
        &lt;option data-item-id="300" value="location_field_type"&gt;Location based input&lt;/option&gt;
      &lt;/select&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div id="text_field_validation_type" class="field trigger-option group100" style="display: none;"&gt;
    &lt;div class="label"&gt;&lt;label&gt;Input validation&lt;/label&gt;&lt;/div&gt;
    &lt;div class="input"&gt;
      &lt;select&gt;
        &lt;option data-item-id="0" data-displayfield="text_raw" value="none"&gt;No validation required&lt;/option&gt;
        &lt;option data-item-id="500" value="text_field_type"&gt;Validation required&lt;/option&gt;
      &lt;/select&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div id="dropdown_field_type" class="field trigger-option group200" style=""&gt;
    &lt;div class="label"&gt;&lt;label&gt;Data source for dropdown list&lt;/label&gt;&lt;/div&gt;
    &lt;div class="input"&gt;
      &lt;select&gt;
        &lt;option data-item-id="6" data-displayfield="dropdown_static" value="none"&gt;Populate with list items specified here&lt;/option&gt;
        &lt;option data-item-id="7" data-displayfield="dropdown_dynamic" value="none"&gt;Retrieve list items from my service&lt;/option&gt;
      &lt;/select&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>The dropdown menu are like this and I can successfully choose it for the first menu after trying many ways using this code.</p>
  
  <pre><code>wait.until(ec.visibility_of_element_located((By.XPATH, "//select[option[@value='text_field_validation_type']]"))).click()
  browser.find_element_by_xpath(".//option[text()[contains(.,'Dropdown list')]]").click()
  </code></pre>
  
  <p>But the problems appear when I need to select the sub menu.</p>
  
  <p>I have tried many ways (also try using Select and ActionChains) and nothing works. </p>
  
  <p>My code using Select:</p>
  
  <pre><code>select_element = browser.find_element_by_xpath("//select[option[@value='text_field_validation_type']]")
  select = Select(select_element)
  select.select_by_index(1)
  
  select_element = browser.find_element_by_xpath("//select[option[@data-item-id='6']]")
  select = Select(select_element)
  select.select_by_index(1)
  </code></pre>
  
  <p>My other code:</p>
  
  <pre><code>wait.until(ec.visibility_of_element_located((By.XPATH, "//select[option[@data-item-id ='6']]"))).click()
  browser.find_element_by_xpath(".//option[@data-displayfield='dropdown_dynamic']").click()
  </code></pre>
  
  <p>I try many ways with changing the xpath value but nothing works and my environment doesnt support ActionChains.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I found out that I need to click the dropdown first, then I can use the select function, but it still only work on first menu. This is the code.</p>
  
  <pre><code>wait.until(ec.visibility_of_element_located((By.XPATH, "//select[option[@value='text_field_validation_type']]"))).click()
  sleep(2)
  select_element = browser.find_element_by_xpath("//select[option[@value='dropdown_field_type']]")
  select = Select(select_element)
  #select.select_by_index(1)
  select.select_by_visible_text('Dropdown list')
  </code></pre>
  
  <p>I also notice that on the dynamic dropdown submenu, they use another div with <code>style="display:none;"</code> when it's not the dropdown submenu of my selected menu, is it affected my problem? I add HTML menu a little.</p>
  
  <p>Can someone help me? Thank you very much.</p>