nutship
nutship

Reputation: 4924

How to click an element visible after hovering with selenium?

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?

Upvotes: 7

Views: 13435

Answers (1)

alecxe
alecxe

Reputation: 473803

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.

Upvotes: 11

Related Questions