python - 无法弄清楚 python selenium webdriver move_to_element 功能

标签 python selenium webdriver mouseover

我查看了许多示例以及 ActionChains 的源代码,似乎正在使用其他示例中建议的代码来实现悬停功能,但我仍然无法克服这个异常。代码如下:

menu = browser.find_element_by_xpath("//nav/ul/li/a[@href='#'][.='Profile']")
hover = ActionChains(webdriver).move_to_element(menu)
hover.perform()

异常(exception)情况是:

Traceback (most recent call last):
File "./test.py", line 56, in <module>
hov.perform()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/common/action_chains.py", line 44, in perform
action()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/common/action_chains.py", line 201, in <lambda>
self._driver.execute(Command.MOVE_TO, {'element': to_element.id}))
AttributeError: 'module' object has no attribute 'execute'

起初,我认为它不起作用,因为元素上没有 id 属性,但我确认情况并非如此(find_element_by_xpath 确实返回了正确的元素并且分配了某种{unique id}) .我的 Python 技能很初级,但我需要调整我正在处理的测试脚本。我确定我只是不明白这个错误。

感谢您的帮助!

最佳答案

ActionChains 的第一个参数是您用来控制浏览器的驱动程序实例,即本例中的browser。尝试以下操作:

menu = browser.find_element_by_xpath("//nav/ul/li/a[@href='#'][.='Profile']")
hover = ActionChains(browser).move_to_element(menu)
hover.perform()

关于python - 无法弄清楚 python selenium webdriver move_to_element 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15281887/

相关文章:

python - 使用 ctypes : undefined symbol 将 C++ 函数导出到 python

java - 由于我将 Selenium 降级到 2.53.1,当我尝试使用 Firefox 访问本地主机页面时出现错误

python - selenium.common.exceptions.TimeoutException : Message: Timed out waiting for page to load using IEDriverServer and Internet Explorer through Selenium

java - 线程中的异常 "main"java.lang.IllegalStateException :The path to the driver executable must be set by the : system property

python - 将复选框数据发布到 Django 中的 View

python - 接听 twilio 电话时收到通知

python - 在 Python 中动态创建列表和字典。我正在尝试用 Python 创建数组

c# - Selenium Webdriver c# 无需等待页面加载

java - Webdriver firefox-bin 使用过多的 CPU

javascript - 将元素拖放到特定位置 - Selenium、WebDriverJS