【发布时间】:2011-08-17 01:51:55
【问题描述】:
我试图在 ActionChains 中使用 move_to_element 触发鼠标悬停事件,但无法正常工作。任何帮助表示赞赏。谢谢。
【问题讨论】:
-
试试 actor.py:gist.github.com/2036553——它可以让你直接调用动作,而不是存储它们,然后调用 'perform'。
我试图在 ActionChains 中使用 move_to_element 触发鼠标悬停事件,但无法正常工作。任何帮助表示赞赏。谢谢。
【问题讨论】:
from selenium.webdriver.common.action_chains import ActionChains
ActionChains(drivers).move_to_element(drivers.find_element_by_id('element_id')).click().perform()
如果你想选择任何值,
menu1 = drivers.find_element_by_xpath('html/path/of/select/box')
sub_menu0 = drivers.find_element_by_xpath('html/path/of/selected/option')
clickon = drivers.find_element_by_xpath(path/of/option/where/you/want/to/click)
action = ActionChains(drivers)
action.move_to_element(menu1)
action.move_to_element(sub_menu0)
action.click(clickon)
action.perform()
【讨论】:
drivers 一定只是糟糕的命名约定
driver 而不是drivers,因为s 意味着多个对象(例如,一个列表,例如@987654327 @ 对象与只有一个)。但除此之外,这个答案非常很有帮助,因为许多其他黑客,包括诉诸从 javascript 调用 click 对我不起作用,但这个对我有用。
我今天也一直在玩 python 中的 ActionChains,并意识到 double_click 不能仅单击。那么你的代码是什么样的。要进行任何操作更改,您必须运行 perform。
def setUp(self):
self.webdriver = webdriver.Ie()
self.mouse = webdriver.ActionChains(self.webdriver)
self.webdriver.get("http://foo")
def test_webdriver(self):
mouse = self.mouse
wd = self.webdriver
wd.implicitly_wait(10)
element = wd.find_element_by_xpath("//div[@title='Create Page']")
mouse.move_to_element(element).perform()
【讨论】:
在我从 selenium 导入 actionchains 之前,我收到了一个 ActionChains is not defined 错误。然后我就可以使用 actions.move_to_element() 和 actions.click()
from selenium.webdriver.common.action_chains import ActionChains
【讨论】: