我尝试鼠标悬停在一个可见元素上,然后点击一个隐藏的子菜单项。move_to_element()
似乎没有使用ChromeDriver。但是,在运行代码时没有任何例外,只是操作没有发生。
我还在操作和sleep()
之间尝试了webDriverWait
,它显示了运行代码的超时。我使用Chrome56.0和python2.7和Selenium3.0.2。
下面是HTML代码
<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>
以下是我测试用例的一部分
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()
发布于 2017-03-07 05:53:07
谢谢你们的帮助。我终于发现,如果物理游标在浏览器窗口中,moveToElement()
就不能工作。这是ChromeDriver已知的一个问题。
https://bugs.chromium.org/p/chromedriver/issues/detail?id=605
发布于 2017-03-06 11:27:38
尝试下面的代码并让我知道结果:
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()
这应该在mainmenu
元素上执行鼠标悬停,直到submenu
元素的出现和可点击性。
发布于 2019-10-17 13:15:42
我遇到了一个类似的问题,并通过使用move_to_element_with_offset()
而不是move_to_element()
来解决它。将move_to_element(myElement)
调用更改为:
move_to_element_with_offset(myElement, 0, 0) # 0, 0 specifies no offset
https://stackoverflow.com/questions/42623590
复制相似问题