我查看了许多示例以及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确实返回正确的元素,并且有某种{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()