Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python selenium : move_to_element() not working

I am trying mouse hover action on a visible element and then click on a hidden sub-menu item. move_to_element() does not seem to be working with ChromeDriver. However, there are no exceptions on running the code, just the action isn't happening.

I have also tried sleep() between actions and webDriverWait which shows timeout on running the code. I am using chrome 56.0 with python 2.7 and selenium 3.0.2.

Following is the HTML code

 <a class="dropdown-toggle" href="about-us.html" data-toggle="dropdown" role="button" aria-expanded="false">
 About
 <i class="caret"></i>
 </a>   

<li>
<a href="about.html">Introduction</a>
</li> 

Following is part of my test case

from selenium import webdriver

from selenium.webdriver.common.action_chains import ActionChains


   mainmenu = driver.find_element_by_xpath("path_to_about_element")
   submenu =driver.find_element_by_xpath("path_to_introduction_element")
   action=ActionChains(driver)
   action.move_to_element(mainmenu)        
   action.move_to_element(submenu)        
   action.click().perform()
like image 851
renuka Avatar asked Dec 14 '22 00:12

renuka


2 Answers

Try below code and let me know the result:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC

mainmenu = driver.find_element_by_link_text("About")
action=ActionChains(driver)
action.move_to_element(mainmenu).perform()
submenu = wait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Introduction")))
submenu.click()

This should perform mouse hovering over mainmenu element and wait until submenu element presence and clickability

like image 186
Andersson Avatar answered Dec 16 '22 14:12

Andersson


Thanks for your help guys. I finally figured out that moveToElement() doesn't work if physical cursor is inside the browser window. It is a known issue with ChromeDriver.

https://bugs.chromium.org/p/chromedriver/issues/detail?id=605

like image 28
renuka Avatar answered Dec 16 '22 13:12

renuka