question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question]: How to drag and drop an element by offset

See original GitHub issue

Your question

Is there a way to drag and drop an element by offset with playwright? Something similar to this: drag_and_drop_by_offset(source, x_offset, y_offset).

The page.drag_and_drop(source, target, **kwargs) (doc) method of playwright seems it can drag a source element into a target element. But how can I drag an element by offset?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
davusescommented, Jun 1, 2022

@davuses can you just do a mouse down, mouse move & mouse up then? Can you share a link for the website for me to play with?

Actually I have managed to do this by combining page.mouse.move(), page.mouse.down() and page.mouse.up() methods. The code to achieve this is as follows:

box = elem_to_drag.bounding_box()
assert box is not None
# move mouse to the center of the element
page.mouse.move(box["x"] + box["width"] / 2, box["y"] + box["height"] / 2)
page.mouse.down()
page.mouse.move(box["x"] + x_offset, box["y"], steps=10)
page.mouse.up()

It did the job, could seem a bit redundant though.

0reactions
baptisteArnocommented, Jul 1, 2022

You can also do this:

const draggableElementSelector = '...'
await page.dragAndDrop(
    draggableElementSelector,
    draggableElementSelector,
    { targetPosition: { x: 0, y: 80 }, force: true }
  )

It will drag an element with an yOffset of 80

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Perform Drag and Drop Offset Method in Selenium ...
This method is to click and hold the web element in the source location and then release the mouse in the specified location....
Read more >
How to Drag and Drop in Selenium WebDriver (EXAMPLE)
Scenario 1: BANK element is dragged and dropped on the specific cell by DragAndDrop method. In the following code, we launch the given...
Read more >
How to Drag and Drop in Selenium using Action ... - Tools QA
dragAndDropBy(WebElement source, int xOffset, int yOffset): This method clicks & holds the source element and moves by a given offset, then ...
Read more >
Protractor dragAndDrop by offset Location of Element
The way you can edit start location is through the mouseMove() method though, shown in one of my other questions here. It is...
Read more >
How to do the basic Drag and Drop action - TestProject Forum
First I will freeze the element I want to drag and I will use the action “Angular drag and drop”:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found