Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 465
nutship Avatar asked May 30 '13 08:05

nutship


People also ask

How do you mouse hover and click the element in Selenium?

The first step here would be to locate the main menu (AKA parent menu). Once that is done, the second step is to locate the desired element (child element) from the available options in the sub-menu. The final step would be to click on that child element.

How do you locate an element that is visible only by mouse hover in Selenium Webdriver Python?

click(); MainMenuBTN = element that becomes visible when you hover the mouse over it.

Which method is used to click on a submenu which is only visible on mouse hover on menu?

After hovering on the menu, we shall select a sub-menu with the help of the click method.


1 Answers

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.

like image 60
alecxe Avatar answered Oct 10 '22 23:10

alecxe