无法理解 Python Selenium Webdriver 中 move_to_element 功能的作用。

4

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

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

而异常情况是:

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确实返回了正确的元素,并且有一些{唯一id}被分配给它)。我的Python技能相当基础,但我需要适应正在进行的测试脚本。我相信我只是不理解这个错误。
谢谢你的帮助!
1个回答

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

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

是的,那真是我犯傻了。 :-) 发现得好! - user1762591

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接