4

I'm looking to automate my web browser to rotate a map created using mapbox, however this can only be done by holding the right mouse down and panning around.

I know there is a click and hold option for the left click, but is there anything similar for right click? At present I can only do a single right click using context click

6
  • 1
    I hope this can answer your query. Link Commented Sep 14, 2020 at 12:08
  • Unfortunately not! I need the right click to stay held down whilst I pan around Commented Sep 15, 2020 at 10:57
  • You can use TouchActions like touchAc = TouchActions(driver) touchAc.tap_and_hold(100, 700).scroll(100, 800).perform() Commented Sep 15, 2020 at 13:14
  • That doesn't right click sadly Commented Sep 15, 2020 at 13:59
  • actions = ActionChains(driver) actions.context_click(element).perform() You can right click using this. Commented Sep 16, 2020 at 4:10

1 Answer 1

2

Unfortunately, this is not supported, but you can modify the source code of selenium in your site-packages directory, it's pretty straightforward since all you have to do is change the parameter for the pointer event in the click_and_hold method, to the same parameter as the pointer event in the context_click method, and then create a custom function for that.

Here's what I managed to do:

File 1: Locate and edit this file in
Lib/site-packages/selenium/webdriver/common/actions/pointer_actions.py

Add this function to the "PointerActions" class:

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):
        ...

File 2: Locate and edit this file in
Lib/site-packages/selenium/webdriver/common/action_chains.py

Add this function to the "ActionChains" class:

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):
        ...

If the .release() function is not able to release the right mouse button after holding, you should add a parameter to the .release() function by making these changes:

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):
        ...
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! But I have to add, that also need to change release function in action_chains.py. I just created right_release() function, that do the same, but with right button.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.