Reputation: 180
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()
Upvotes: 5
Views: 32892
Reputation: 88
I also just realized, that there is a difference between:
ActionChains(driver).move_to_element_with_offset(element, 0, 0).perform()
and
driver.execute_script('arguments[0].scrollIntoView();', element)
The former resulted in an exception telling me that the move target was out of bound.
The latter seems to be more relieable and also faster.
Upvotes: 1
Reputation: 3243
This may work:-
driver.execute_script("document.getElementById('myelementid').scrollIntoView();")
Upvotes: 4
Reputation: 3560
I run into a similar issue and solved it by using move_to_element_with_offset()
instead of move_to_element()
. Change the move_to_element(myElement)
call into:
move_to_element_with_offset(myElement, 0, 0) # 0, 0 specifies no offset
Upvotes: 1
Reputation: 180
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
Upvotes: 5
Reputation: 7708
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()
Upvotes: 0
Reputation: 52665
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
Upvotes: 6