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?
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.
click(); MainMenuBTN = element that becomes visible when you hover the mouse over it.
After hovering on the menu, we shall select a sub-menu with the help of the click method.
I bet you should wait for the element until it becomes visible.
Three options:
time.sleep(n)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With