python 硒:move_to_element() 不工作

新手上路,请多包涵

我正在尝试在可见元素上进行鼠标悬停操作,然后单击隐藏的子菜单项。 move_to_element() 似乎不适用于 ChromeDriver。但是,运行代码没有异常,只是操作没有发生。

我也尝试过 sleep() 在操作和 webDriverWait --- 之间显示运行代码超时。我将 chrome 56.0 与 python 2.7 和 selenium 3.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()

原文由 renuka 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 910
2 个回答

试试下面的代码,让我知道结果:

 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 元素存在和可点击性

原文由 Andersson 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题