11

I'm trying to automate an administration task, so far I have made selenium to click on an element to show a dropdown menu.

enter image description here

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.

Code:

 driver = webdriver.Chrome()
 driver.implicitly_wait(10)
 driver.get(url)
 doc = driver.find_element_by_css_selector('td.ms-vb-title > 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()

Error:

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'

As you can see the code waits a lot to get the element loaded. I've also tried to set the element's is_displayed property to True but didn't work neither.

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)

Edit:

The menu variable is not the menu itself it's one of the spans that are elements of the menu, doc is the Perfil html element getting clicked to show the dropdown.

Edit 2:

Inspecting the DOM on chrome tools, when you click a doc 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.

Final Edit:

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.

8
  • 1
    Why are you sleeping on both sides of the menu element lookup? If the element isn't visible when you retrieve the page element, webdriver won't be able to click it, no matter how long you wait.
    – Silas Ray
    Sep 12, 2012 at 19:54
  • @sr2222 The page is retrieved when you call driver.get(), the dropdown is displayed on the screen and present on the dom tree when you do doc.click(), i just eliminated the possibility of the dropdown menu not being loaded as fast as code is executed. The menu element is present and visible, at least to humans, i can guarantee that.
    – loki
    Sep 12, 2012 at 20:06
  • Try extracting doc again after clicking and waiting for the menu to display, and compare the id attribute of the objects from before and after. I have a sneaking suspicion that the selenium server is actually referencing a cached webelement object on your second element extraction (since your click action doesn't trigger a page load event, the page object doesn't go stale).
    – Silas Ray
    Sep 12, 2012 at 20:28
  • Are you sure the span is what is clickable in this case? In order to be clickable, an element must have dimensions and be visible. You might have to click some element in the span rather than the span itself.
    – Silas Ray
    Sep 12, 2012 at 21:01
  • @sr2222 <span type="option" text="Eliminar" ...></span>, it has nothing inside of it.
    – loki
    Sep 12, 2012 at 21:05

2 Answers 2

8

Why don't you select an option like this

el = driver.find_element_by_id('id_of_select')
for option in el.find_elements_by_tag_name('option'):
    if option.text == 'The Options I Am Looking For':
        option.click() # select() in earlier versions of webdriver

If your click is not firing an ajax call to populate your list, you don't actually need to execute the click.

2
  • @Nllesh Sharma I used this approach but I am getting an error that web element object is not iterable. How you are iterating over it in the for loop if it is not iterable
    – abhi
    Apr 18, 2013 at 9:44
  • @abhi are you sure using find_elementSSSSS_by_tag_name()? (just to have sure you saw that S hehe) May 11, 2013 at 17:43
1

You need to find the link of the target. You don't really click elements, you click links... (or rather, you click elements with links inside them). That being said, the most sure fire way to click a link is to isolate the link element.

frame = driver.find_element_by_id('this_is_your_frame_name') 
links = frame.find_elements_by_xpath('.//a')
links[1].click()

or alternatively,

for link in links:
    if link.text() == "Administratar Permisos":
        link.click()

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.