gpt4 book ai didi

python Selenium : move_to_element() not working

转载 作者:行者123 更新时间:2023-12-01 12:22:44 24 4
gpt4 key购买 nike

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

我也试过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()

最佳答案

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

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

关于 python Selenium : move_to_element() not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42623590/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com