python selenium : move_to_element() not working

2019-06-05 21:35发布

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

3条回答
Luminary・发光体
2楼-- · 2019-06-05 22:11

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

查看更多
仙女界的扛把子
3楼-- · 2019-06-05 22:18

Use the following code and let me know if still face the same issue :

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).move_to_element(submenu).click().build().perform()      
查看更多
叛逆
4楼-- · 2019-06-05 22:28

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

查看更多
登录 后发表回答