如何在Selenium Python中实现右键点击并按住

4

我希望能够自动化我的Web浏览器,来旋转一个使用Mapbox创建的地图,然而这只能通过按住右键并围绕着地图移动来完成。

我知道左键有一个点击并保持选项,但是右键有类似的选项吗?目前,我只能使用上下文菜单进行单击右键。


1
我希望这可以回答您的查询。链接 - Dilip Meghwal
很遗憾,不行!我需要右键一直按住,才能在周围平移。 - GIS_User2
你可以使用TouchActions,例如touchAc = TouchActions(driver) touchAc.tap_and_hold(100, 700).scroll(100, 800).perform() - Dilip Meghwal
很遗憾,这不能右键。 - GIS_User2
actions = ActionChains(driver) actions.context_click(element).perform() 你可以使用这个来进行右键点击。 - Dilip Meghwal
按住右键是我的难点。类似于click_and_hold,我想知道是否有类似于context_and_hold的东西。 - GIS_User2
1个回答

2
很遗憾,这是不支持的,但是您可以修改selenium源代码中的指针事件参数,使其与context_click方法中的相同参数,这样做非常简单。然后为此创建一个自定义函数。请注意,保留html标签。
以下是我所做的内容:
文件1:在“Lib/site-packages/selenium/webdriver/common/actions/pointer_actions.py”中定位并编辑此文件。
将此函数添加到“PointerActions”类中。
class PointerActions(Interaction):
    def move_to_location(self, x, y):
        ...
    def click(self, element=None):
        ...
    def context_click(self, element=None):
        ...

    # --- Add this function
    def context_click_and_hold(self, element=None):
        if element:
            self.move_to(element)
        self.pointer_down(MouseButton.RIGHT)
        return self
    # ---
    
    def click_and_hold(self, element=None):
        ...

    def release(self):
        ...
    def double_click(self, element=None):
        ...

文件2:在 Lib/site-packages/selenium/webdriver/common/action_chains.py 中找到并编辑此文件。

将此函数添加到“ActionChains”类中:

class ActionChains(object):
    def __init__(self, driver, duration=250):
        ...
    def click(self, on_element=None):
        ...

    # --- Add this function
    def context_click_and_hold(self, on_element=None):
        if on_element:
            self.move_to_element(on_element)

        self.w3c_actions.pointer_action.context_click_and_hold()
        self.w3c_actions.key_action.pause()

        return self
    # ---
    
    def click_and_hold(self, on_element=None):
        ...
    def context_click(self, on_element=None):
        ...

如果 .release() 函数无法在按住右键后释放右键,您应该通过进行以下更改向 .release() 函数添加一个参数:

class PointerActions(Interaction):
    def click_and_hold(self):
        ...

    def release(self, button="left"):
        button = MouseButton.LEFT if button == "left" else MouseButton.RIGHT
        self.pointer_up(button)
        return self

    def double_click(self):
        ...

class ActionChains(object):
    def pause(self):
        ...

    def release(self, button="left", on_element=None):
        """
        Releasing a held mouse button on an element.

        :Args:
         - on_element: The element to mouse up.
           If None, releases on current mouse position.
        """
        if on_element:
            self.move_to_element(on_element)

        self.w3c_actions.pointer_action.release(button)
        self.w3c_actions.key_action.pause()

        return self

    def send_keys(self):
        ...

非常感谢!但我必须补充说明,还需要更改action_chains.py中的release函数。我刚刚创建了right_release()函数,它可以执行相同的操作,但使用右键。 - An Ri

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