Best Selenium code snippet using Selenium.WebDriver.PointerActions.click_and_hold
pointer_actions.rb
Source:pointer_actions.rb  
...27      end28      #29      # Presses (without releasing) at the current location of the PointerInput device. This is equivalent to:30      #31      #   driver.action.click_and_hold(nil)32      #33      # @example Clicking and holding at the current location34      #35      #    driver.action.pointer_down(:left).perform36      #37      # @param [Selenium::WebDriver::Interactions::PointerPress::BUTTONS] button the button to press.38      # @param [Symbol || String] device optional name of the PointerInput device with the button39      #   that will be pressed40      # @return [W3CActionBuilder] A self reference.41      #42      def pointer_down(button, device: nil)43        button_action(button, action: :create_pointer_down, device: device)44      end45      #46      # Releases the pressed mouse button at the current mouse location of the PointerInput device.47      #48      # @example Releasing a button after clicking and holding49      #50      #    driver.action.pointer_down(:left).pointer_up(:left).perform51      #52      # @param [Selenium::WebDriver::Interactions::PointerPress::BUTTONS] button the button to release.53      # @param [Symbol || String] device optional name of the PointerInput device with the button that will54      #   be released55      # @return [W3CActionBuilder] A self reference.56      #57      def pointer_up(button, device: nil)58        button_action(button, action: :create_pointer_up, device: device)59      end60      #61      # Moves the mouse to the middle of the given element. The element is scrolled into62      # view and its location is calculated using getBoundingClientRect.  Then the63      # mouse is moved to optional offset coordinates from the element.64      #65      # This is adapted to be backward compatible from non-W3C actions. W3C calculates offset from the center point66      # of the element67      #68      # Note that when using offsets, both coordinates need to be passed.69      #70      # @example Scroll element into view and move the mouse to it71      #72      #   el = driver.find_element(id: "some_id")73      #   driver.action.move_to(el).perform74      #75      # @example76      #77      #   el = driver.find_element(id: "some_id")78      #   driver.action.move_to(el, 100, 100).perform79      #80      # @param [Selenium::WebDriver::Element] element to move to.81      # @param [Integer] right_by Optional offset from the top-left corner. A negative value means82      #   coordinates to the left of the element.83      # @param [Integer] down_by Optional offset from the top-left corner. A negative value means84      #   coordinates above the element.85      # @param [Symbol || String] device optional name of the PointerInput device to move.86      # @return [W3CActionBuilder] A self reference.87      #88      def move_to(element, right_by = nil, down_by = nil, device: nil)89        pointer = get_pointer(device)90        # New actions offset is from center of element91        if right_by || down_by92          size = element.size93          left_offset = (size[:width] / 2).to_i94          top_offset = (size[:height] / 2).to_i95          left = -left_offset + (right_by || 0)96          top = -top_offset + (down_by || 0)97        else98          left = 099          top = 0100        end101        pointer.create_pointer_move(duration: default_move_duration,102                                    x: left,103                                    y: top,104                                    element: element)105        tick(pointer)106        self107      end108      #109      # Moves the mouse from its current position by the given offset.110      # If the coordinates provided are outside the viewport (the mouse will111      # end up outside the browser window) then the viewport is scrolled to112      # match.113      #114      # @example Move the mouse to a certain offset from its current position115      #116      #    driver.action.move_by(100, 100).perform117      #118      # @param [Integer] right_by horizontal offset. A negative value means moving the mouse left.119      # @param [Integer] down_by vertical offset. A negative value means moving the mouse up.120      # @param [Symbol || String] device optional name of the PointerInput device to move121      # @return [W3CActionBuilder] A self reference.122      # @raise [MoveTargetOutOfBoundsError] if the provided offset is outside the document's boundaries.123      #124      def move_by(right_by, down_by, device: nil)125        pointer = get_pointer(device)126        pointer.create_pointer_move(duration: default_move_duration,127                                    x: Integer(right_by),128                                    y: Integer(down_by),129                                    origin: Interactions::PointerMove::POINTER)130        tick(pointer)131        self132      end133      #134      # Moves the mouse to a given location in the viewport.135      # If the coordinates provided are outside the viewport (the mouse will136      # end up outside the browser window) then the viewport is scrolled to137      # match.138      #139      # @example Move the mouse to a certain position in the viewport140      #141      #    driver.action.move_to_location(100, 100).perform142      #143      # @param [Integer] x horizontal position. Equivalent to a css 'left' value.144      # @param [Integer] y vertical position. Equivalent to a css 'top' value.145      # @param [Symbol || String] device optional name of the PointerInput device to move146      # @return [W3CActionBuilder] A self reference.147      # @raise [MoveTargetOutOfBoundsError] if the provided x or y value is outside the document's boundaries.148      #149      def move_to_location(x, y, device: nil)150        pointer = get_pointer(device)151        pointer.create_pointer_move(duration: default_move_duration,152                                    x: Integer(x),153                                    y: Integer(y),154                                    origin: Interactions::PointerMove::VIEWPORT)155        tick(pointer)156        self157      end158      #159      # Clicks (without releasing) in the middle of the given element. This is160      # equivalent to:161      #162      #   driver.action.move_to(element).click_and_hold163      #164      # @example Clicking and holding on some element165      #166      #    el = driver.find_element(id: "some_id")167      #    driver.action.click_and_hold(el).perform168      #169      # @param [Selenium::WebDriver::Element] element the element to move to and click.170      # @param [Symbol || String] device optional name of the PointerInput device to click with171      # @return [W3CActionBuilder] A self reference.172      #173      def click_and_hold(element = nil, device: nil)174        move_to(element, device: device) if element175        pointer_down(:left, device: device)176        self177      end178      #179      # Releases the depressed left mouse button at the current mouse location.180      #181      # @example Releasing an element after clicking and holding it182      #183      #    el = driver.find_element(id: "some_id")184      #    driver.action.click_and_hold(el).release.perform185      #186      # @param [Symbol || String] device optional name of the PointerInput device with the button187      #   that will be released188      # @return [W3CActionBuilder] A self reference.189      #190      def release(device: nil)191        pointer_up(:left, device: device)192        self193      end194      #195      # Clicks in the middle of the given element. Equivalent to:196      #197      #   driver.action.move_to(element).click198      #199      # When no element is passed, the current mouse position will be clicked.200      #201      # @example Clicking on an element202      #203      #    el = driver.find_element(id: "some_id")204      #    driver.action.click(el).perform205      #206      # @example Clicking at the current mouse position207      #208      #    driver.action.click.perform209      #210      # @param [Selenium::WebDriver::Element] element An optional element to click.211      # @param [Symbol || String] device optional name of the PointerInput device with the button212      #   that will be clicked213      # @return [W3CActionBuilder] A self reference.214      #215      def click(element = nil, device: nil)216        move_to(element, device: device) if element217        pointer_down(:left, device: device)218        pointer_up(:left, device: device)219        self220      end221      #222      # Performs a double-click at middle of the given element. Equivalent to:223      #224      #   driver.action.move_to(element).double_click225      #226      # When no element is passed, the current mouse position will be double-clicked.227      #228      # @example Double-click an element229      #230      #    el = driver.find_element(id: "some_id")231      #    driver.action.double_click(el).perform232      #233      # @example Double-clicking at the current mouse position234      #235      #    driver.action.double_click.perform236      #237      # @param [Selenium::WebDriver::Element] element An optional element to move to.238      # @param [Symbol || String] device optional name of the PointerInput device with the button239      #   that will be double-clicked240      # @return [W3CActionBuilder] A self reference.241      #242      def double_click(element = nil, device: nil)243        move_to(element, device: device) if element244        click(device: device)245        click(device: device)246        self247      end248      #249      # Performs a context-click at middle of the given element. First performs250      # a move_to to the location of the element.251      #252      # When no element is passed, the current mouse position will be context-clicked.253      #254      # @example Context-click at middle of given element255      #256      #   el = driver.find_element(id: "some_id")257      #   driver.action.context_click(el).perform258      #259      # @example Context-clicking at the current mouse position260      #261      #    driver.action.context_click.perform262      #263      # @param [Selenium::WebDriver::Element] element An element to context click.264      # @param [Symbol || String] device optional name of the PointerInput device with the button265      #   that will be context-clicked266      # @return [W3CActionBuilder] A self reference.267      #268      def context_click(element = nil, device: nil)269        move_to(element, device: device) if element270        pointer_down(:right, device: device)271        pointer_up(:right, device: device)272        self273      end274      #275      # A convenience method that performs click-and-hold at the location of the276      # source element, moves to the location of the target element, then277      # releases the mouse.278      #279      # @example Drag and drop one element onto another280      #281      #   el1 = driver.find_element(id: "some_id1")282      #   el2 = driver.find_element(id: "some_id2")283      #   driver.action.drag_and_drop(el1, el2).perform284      #285      # @param [Selenium::WebDriver::Element] source element to emulate button down at.286      # @param [Selenium::WebDriver::Element] target element to move to and release the287      #   mouse at.288      # @param [Symbol || String] device optional name of the PointerInput device with the button289      #   that will perform the drag and drop290      # @return [W3CActionBuilder] A self reference.291      #292      def drag_and_drop(source, target, device: nil)293        click_and_hold(source, device: device)294        move_to(target, device: device)295        release(device: device)296        self297      end298      #299      # A convenience method that performs click-and-hold at the location of300      # the source element, moves by a given offset, then releases the mouse.301      #302      # @example Drag and drop an element by offset303      #304      #   el = driver.find_element(id: "some_id1")305      #   driver.action.drag_and_drop_by(el, 100, 100).perform306      #307      # @param [Selenium::WebDriver::Element] source Element to emulate button down at.308      # @param [Integer] right_by horizontal move offset.309      # @param [Integer] down_by vertical move offset.310      # @param [Symbol || String] device optional name of the PointerInput device with the button311      #   that will perform the drag and drop312      # @return [W3CActionBuilder] A self reference.313      #314      def drag_and_drop_by(source, right_by, down_by, device: nil)315        click_and_hold(source, device: device)316        move_by(right_by, down_by, device: device)317        release(device: device)318        self319      end320      private321      def button_action(button, action: nil, device: nil)322        pointer = get_pointer(device)323        pointer.send(action, button)324        tick(pointer)325        self326      end327      def get_pointer(device = nil)328        get_device(device) || pointer_inputs.first329      end...click_and_hold
Using AI Code Generation
1driver.switch_to.frame("iframeResult")2source = driver.find_element(:id, "drag1")3target = driver.find_element(:id, "div2")4actions = Selenium::WebDriver::ActionBuilder.new(driver: driver)5actions.click_and_hold(source).move_to(target).release.perform6driver.switch_to.frame("iframeResult")7source = driver.find_element(:id, "drag1")8target = driver.find_element(:id, "div2")9actions = Selenium::WebDriver::ActionBuilder.new(driver: driver)10actions.click_and_hold(source).move_to(target).release.perform11driver.switch_to.frame("iframeResult")12source = driver.find_element(:id, "drag1")13target = driver.find_element(:id, "div2")14actions = Selenium::WebDriver::ActionBuilder.new(driver: driver)15actions.click_and_hold(source).move_to(target).release.perform16driver.switch_to.frame("iframeResult")17source = driver.find_element(:id, "drag1")18target = driver.find_element(:id, "div2")19actions = Selenium::WebDriver::ActionBuilder.new(driver: driver)20actions.click_and_hold(source).move_to(target).release.performclick_and_hold
Using AI Code Generation
1element = driver.find_element(:name, 'q')2actions = Selenium::WebDriver::ActionBuilder.new(driver)3actions.click_and_hold(element).perform4element = driver.find_element(:name, 'q')5actions = Selenium::WebDriver::ActionBuilder.new(driver)6actions.click_and_hold(element).perform7element = driver.find_element(:name, 'q')8actions = Selenium::WebDriver::ActionBuilder.new(driver)9actions.click_and_hold(element).perform10element = driver.find_element(:name, 'q')11actions = Selenium::WebDriver::ActionBuilder.new(driver)12actions.click_and_hold(element).perform13element = driver.find_element(:name, 'q')14actions = Selenium::WebDriver::ActionBuilder.new(driver)15actions.click_and_hold(element).perform16element = driver.find_element(:name, 'q')17actions = Selenium::WebDriver::ActionBuilder.new(driver)18actions.click_and_hold(element).performclick_and_hold
Using AI Code Generation
1source = driver.find_element(xpath: "//p[contains(text(),'Draggable 1')]")2target = driver.find_element(xpath: "//div[@id='mydropzone']")3action = Selenium::WebDriver::ActionBuilder.new(driver: driver)4action.click_and_hold(source).move_to(target).release.performclick_and_hold
Using AI Code Generation
1action = Selenium::WebDriver::PointerInput.new(:mouse).create_pointer_move(duration: 0, x: source.location.x, y: source.location.y)2action = Selenium::WebDriver::PointerInput.new(:mouse).create_pointer_down(:left)3action = Selenium::WebDriver::PointerInput.new(:mouse).create_pointer_move(duration: 1000, x: target.location.x, y: target.location.y)4action = Selenium::WebDriver::PointerInput.new(:mouse).create_pointer_up(:left)5action = Selenium::WebDriver::Interactions::Builder.new(driver)6action.drag_and_drop_by(source, target.location.x, target.location.y).perform7action = Selenium::WebDriver::Interactions::Builder.new(driver)8action.drag_and_drop(source, target).perform9action = Selenium::WebDriver::Interactions::Builder.new(driver)10action.drag_and_drop_by(source, target.location.x, target.location.y).performclick_and_hold
Using AI Code Generation
1pointer_actions = Selenium::WebDriver::PointerActions.new(driver)2action_builder = Selenium::WebDriver::ActionBuilder.new(driver, pointer_actions)3element = driver.find_element(:xpath, "//input[@name='q']")4action_builder.click_and_hold(element).performclick_and_hold
Using AI Code Generation
1element = driver.find_element(:id, 'textAreaExample')2action = Selenium::WebDriver::ActionBuilder.new(driver)3action.click_and_hold(element)4action = Selenium::WebDriver::ActionBuilder.new(driver)5action.click_and_hold(element)6action = Selenium::WebDriver::ActionBuilder.new(driver)7action.click_and_hold(element)8action = Selenium::WebDriver::ActionBuilder.new(driver)9action.click_and_hold(element)10action = Selenium::WebDriver::ActionBuilder.new(driver)11action.click_and_hold(element)12action = Selenium::WebDriver::ActionBuilder.new(driver)13action.click_and_hold(element)click_and_hold
Using AI Code Generation
1source = driver.find_element(:id, "drag1")2target = driver.find_element(:id, "div2")3pointer_actions = Selenium::WebDriver::PointerActions.new(driver)4pointer_actions.click_and_hold(source).perform5pointer_actions.move_to(target).performclick_and_hold
Using AI Code Generation
1action.drag_and_drop_by(source, target.location.x, target.location.y).perform2action = Selenium::WebDriver::Interactions::Builder.new(driver)3action.drag_and_drop(source, target).perform4action = Selenium::WebDriver::Interactions::Builder.new(driver)5action.drag_and_drop_by(source, target.location.x, target.location.y).performclick_and_hold
Using AI Code Generation
1element = driver.find_element(:id, 'textAreaExample')2action = Selenium::WebDriver::ActionBuilder.new(driver)3action.click_and_hold(element)4action = Selenium::WebDriver::ActionBuilder.new(driver)5action.click_and_hold(element)6action = Selenium::WebDriver::ActionBuilder.new(driver)7action.click_and_hold(element)8action = Selenium::WebDriver::ActionBuilder.new(driver)9action.click_and_hold(element)10action = Selenium::WebDriver::ActionBuilder.new(driver)11action.click_and_hold(element)12action = Selenium::WebDriver::ActionBuilder.new(driver)13action.click_and_hold(element)click_and_hold
Using AI Code Generation
1source = driver.find_element(:id, "drag1")2target = driver.find_element(:id, "div2")3pointer_actions = Selenium::WebDriver::PointerActions.new(driver)4pointer_actions.click_and_hold(source).perform5pointer_actions.move_to(target).performclick_and_hold
Using AI Code Generation
1element = driver.find_element(:id, 'textAreaExample')2action = Selenium::WebDriver::ActionBuilder.new(driver)3action.click_and_hold(element)4action = Selenium::WebDriver::ActionBuilder.new(driver)5action.click_and_hold(element)6action = Selenium::WebDriver::ActionBuilder.new(driver)7action.click_and_hold(element)8action = Selenium::WebDriver::ActionBuilder.new(driver)9action.click_and_hold(element)10action = Selenium::WebDriver::ActionBuilder.new(driver)11action.click_and_hold(element)12action = Selenium::WebDriver::ActionBuilder.new(driver)13action.click_and_hold(element)Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
