How to click an element visible after hovering with selenium?

I want to click the button that is visible after freezing. Its html:

<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() 

This does not work. I have an error related to the last line of element.click(): code element.click():

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

Any suggestions please?

+6
source share
1 answer

I bet you have to wait for the item until it becomes visible.

Three options:

  • call time.sleep(n)
  • use WebDriverWait as he suggested here , here and here

I would go with the second option.

UPD:

On this particular site, hovering through selenium, it 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 this helps.

+9
source

Source: https://habr.com/ru/post/946156/