7

I want to click a button which is visible after hovering. Its html is:

<span class="info"></span>

I used this code:

import selenium.webdriver as webdriver
from selenium.webdriver.common.action_chains import ActionChains

url = "http://example.com"

driver = webdriver.Firefox()
driver.get(url)
element = driver.find_element_by_class_name("info")
hov = ActionChains(driver).move_to_element(element)
hov.perform()
element.click()

It's not working though. I got a an error connected with the last line of code element.click():

selenium.common.exceptions.ElementNotVisibleException: Message: \
u'Element is not currently visible and so may not be interacted with' 

Any suggestions please?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
nutship
  • 4,624
  • 13
  • 47
  • 64

1 Answers1

11

I bet you should wait for the element until it becomes visible.

Three options:

  • call time.sleep(n)
  • use WebDriverWait like it's suggested here and here

I'd go with the second option.

UPD:

On this particular site hovering via selenium didn't work at all, so the only option was to click on the button using js via execute_script:

driver.execute_script('$("span.info").click();')

Hope that helps.

Raunak Thomas
  • 1,393
  • 1
  • 12
  • 28
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thanks for suggestions but I've tried `time.sleep` actually and it seems it's not the case. – nutship May 30 '13 at 08:45
  • Ok, `WebDriverWait` should work, let me know if you'll have problems with it. – alecxe May 30 '13 at 08:49
  • Sorry for late answer but needed to read your links through. I still think my issue differs as the element/button in my case is visible/accessible all the time. So I can hover over it instantly as the page loads. There is no ajax/js code execution in between. I'm just unable to correctly localize the elements either with `find_element_by` or `xpath`. – nutship May 30 '13 at 10:03
  • got it, could you provide the link to the page, so I can find a workaround for you? – alecxe May 30 '13 at 10:09
  • I'd love to. For 'security' reasons I think I'm better off not posting it. Can we possibly contact via email? – nutship May 30 '13 at 10:12