How to click an element visible after hovering with selenium?


Question

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?

1
6
12/10/2014 4:32:50 PM

Accepted Answer

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.

9
5/18/2018 6:00:26 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon