title: Click on a dropdown element menu with Selenium Webdriver
body: <p>I'm trying to automate an administration task, so far I have made selenium to click on an element to show a dropdown menu.</p>
  
  <p><img src="https://i.stack.imgur.com/lvWNI.png" alt="enter image description here"></p>
  
  <p>When it comes the time to click on one of those menu elements I've got an error saying that the element must be displayed.</p>
  
  <p>Code:</p>
  
  <pre><code> driver = webdriver.Chrome()
   driver.implicitly_wait(10)
   driver.get(url)
   doc = driver.find_element_by_css_selector('td.ms-vb-title &gt; table')
   try:
      doc.click()
      time.sleep(4)
      menu = driver.find_element_by_xpath('//menu/span[5]')
      time.sleep(4)
      print dir(menu)
      menu.click()
   except:
      traceback.print_exc()
      driver.quit()
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "aprobar_docs.py", line 22, in main
      menu.click()
    File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py",
  line 52, in click
      self._execute(Command.CLICK_ELEMENT)
    File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py",
  line 205, in _execute
      return self._parent.execute(command, params)
    File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
  ine 156, in execute
      self.error_handler.check_response(response)
    File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py"
  , line 147, in check_response
      raise exception_class(message, screen, stacktrace)
  ElementNotVisibleException: Message: u'Element must be displayed to click'
  </code></pre>
  
  <p>As you can see the code waits a lot to get the element loaded. I've also tried to set the element's <code>is_displayed</code> property to <code>True</code> but didn't work neither.</p>
  
  <p>Note: the element that's not displayed is the one on the xpath search, it is present because I've logged it with dir(menu)</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>The <code>menu</code> variable is not the menu itself it's one of the spans that are elements of the menu, <code>doc</code> is the <code>Perfil</code> html element getting clicked to show the dropdown.</p>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>Inspecting the DOM on chrome tools, when you click a <code>doc</code> a new menu gets created on the tree, I don't know if it's because of an ajax call or vanilla js, I don't think it's really that important how it's created. I can't retrieve it from the page and make a python object from it, it's just not being displayed at least on code.</p>
  
  <p><strong>Final Edit:</strong></p>
  
  <p>I ended up executing some JavaScript to make it work. Apparently when Selenium finds the menu item the first element that triggers the menu drop down loses the focus and it makes the menu invisible again, if you don't select a menu item and wait for some time the menu dropdown still is shown, if you try to select one element from the menu the menu disappears.</p>