更新#2(09/27/2020):
问题解决了。
原来,重复的动作是由“ {Action}}:github.com/SeleniumHQ/selenium/issues/6837的“ reset_action”方法中的错误引起的。如果您正在使用Selenium 3.141.59(当前最新版本),则可能会发现上面的链接有用。根据作者的说法,该错误已被关闭,并且(一个更新)“将在下一个版本中发布。”
最后,在没有新发行版的情况下,我解决此问题的方法是将所有操作排队,并在测试结束时仅使用一次“ perform()”。这意味着您不必修改硒模块。例如:
element_1 = driver.find_element_by_css_selector(".nav")
element_2 = driver.find_element_by_css_selector(".nav")
actions = ActionChains(driver)
actions.move_to_element(element_1)
right_mouse_drag_by(100, 100, element_2)
actions.perform()
希望这会有所帮助。
更新#1(09/27/2020):
仔细研究硒实际上如何触发动作后,我如下所示修改了辅助函数。令人惊讶的是,它有效。
但是,也发生了一些奇怪的事情:在辅助函数被重复之前调用的任何操作链方法。知道这是怎么发生的吗?
原始帖子:
这就是问题:硒是惊人的,但是我目前正在研究的项目要求我测试Web画布上的鼠标右键单击并拖动动作。 Selenium并未将其包含在其ActionChains模块中,因为对于大多数网页测试人员而言,这种情况很少见。
现在,我的想法是在测试文件中创建一个辅助函数以执行此操作。我的搜索使我进入了硒中的“ pointer_actions”模块,该模块中提供以下类:
class PointerActions(Interaction):
def __init__(self, source=None):
if source is None:
source = PointerInput(interaction.POINTER_MOUSE, "mouse")
self.source = source
super(PointerActions, self).__init__(source)
def pointer_down(self, button=MouseButton.LEFT):
self._button_action("create_pointer_down", button=button)
def pointer_up(self, button=MouseButton.LEFT):
self._button_action("create_pointer_up", button=button)
def move_to(self, element, x=None, y=None):
if not isinstance(element, WebElement):
raise AttributeError("move_to requires a WebElement")
if x is not None or y is not None:
el_rect = element.rect
left_offset = el_rect['width'] / 2
top_offset = el_rect['height'] / 2
left = -left_offset + (x or 0)
top = -top_offset + (y or 0)
else:
left = 0
top = 0
self.source.create_pointer_move(origin=element, x=int(left), y=int(top))
return self
def move_by(self, x, y):
self.source.create_pointer_move(origin=interaction.POINTER, x=int(x), y=int(y))
return self
def move_to_location(self, x, y):
self.source.create_pointer_move(origin='viewport', x=int(x), y=int(y))
return self
def click(self, element=None):
if element:
self.move_to(element)
self.pointer_down(MouseButton.LEFT)
self.pointer_up(MouseButton.LEFT)
return self
def context_click(self, element=None):
if element:
self.move_to(element)
self.pointer_down(MouseButton.RIGHT)
self.pointer_up(MouseButton.RIGHT)
return self
def click_and_hold(self, element=None):
if element:
self.move_to(element)
self.pointer_down()
return self
def release(self):
self.pointer_up()
return self
def double_click(self, element=None):
if element:
self.move_to(element)
self.click()
self.click()
def pause(self, duration=0):
self.source.create_pause(duration)
return self
def _button_action(self, action, button=MouseButton.LEFT):
meth = getattr(self.source, action)
meth(button)
return self
我的助手功能是:
Act = ActionChains(browser)
# helpers
def right_mouse_drag_by(xoffset, yoffset, on_element=None):
if on_element:
Act.w3c_actions.pointer_action.move_to(on_element)
return right_mouse_drag_by(xoffset, yoffset)
else:
Act.w3c_actions.clear_actions()
Act.w3c_actions.pointer_action.pointer_down(MouseButton.RIGHT)
Act.w3c_actions.pointer_action.move_by(xoffset, yoffset)
Act.w3c_actions.pointer_action.pointer_up(MouseButton.RIGHT)
Act.w3c_actions.perform()
简而言之,自动化结果是,尽管pointer_down,pointer_up和_button_action方法采用了参数“ MouseButton.RIGHT”,但屏幕上发生的事情是当调用辅助函数时,我的指针仍在执行左键单击。
我查看了StackOverflow,几年前有一个类似的问题从未解决?
我真的很感谢任何建议或想法。