0

This is HTML code before having the element in svg.

enter image description here

After that, I pull a new element from the sidebar into svg and HTML code like that enter image description here

So I want to move a new element to new position in svg. Example: Element have a position is (328,201) and I want to move to new position is (0,0) in svg. I have tried another way to move the element in svg but not work. Anyone have an idea to perform

WebElement From = driver.findElement(itemListSVG);

act.moveToElement(From, 0, 0).build().perform();


act.click(From).moveToElement(From, 0, 0).build().perform();
2
  • Is it movable manually?
    – Alexey R.
    Dec 18, 2018 at 9:11
  • Yes, I can move element manual Dec 18, 2018 at 9:14

4 Answers 4

1

What you're doing actually is:

  1. Click an item (click)
  2. Moving a mouse pointer to that item (moveToElement which accepts the target element as its parameter)

There are no actions defined for dragging and dropping the items whatever they are (svg or other types of elements). You should use one of the following approaches:

  1. dragAndDropBy(WebElement source, int xOffset, int yOffset) where you set what you drag and drop and the offset to where you drag and drop
  2. Use clickAndHold(WebElement onElement) that would emulate holding your element with a mouse, then moveByOffset(int xOffset, int yOffset) or moveToElement(WebElement toElement) and then release() that would release your mouse button.
10
  • I have following with 1 but not work: WebElement From = driver.findElement(itemRecipientListSVG); Actions act = new Actions(driver); act.clickAndHold(From).moveByOffset(608, 181).release().build().perform(); Dec 18, 2018 at 10:19
  • @SinhVienIT what are these coordinates 608, 181?
    – Alexey R.
    Dec 18, 2018 at 10:23
  • This position I get from svg by manual Dec 18, 2018 at 10:26
  • And I have to try Actions act = new Actions(driver); act.dragAndDropBy(From, 608, 181).release().build().perform(); and I get error org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document Dec 18, 2018 at 10:27
  • @SinhVienIT these should be the target position offset. That means that if your source svg element has the position say {x=600, y=100}, and target place you'd like to move it to {x=608, y=181}, then your offsets would be {xOffset=8, yOffset=81}. Check that you use the method properly.
    – Alexey R.
    Dec 18, 2018 at 10:28
0

Try adding move to element before the click and hold.

        Actions action = new Actions(driver);
        action.MoveToElement(element);
        action.ClickAndHold().MoveByOffset(offsetX, offsetY).Release();
        action.Build().Perform();
0
0

As I understood when you are trying to move the element you are getting StaleElementReferenceException. Are you handling that? If no try doing following 1. Use explicit wait 2. Use try catch around the code. If it worked let me know else go for another solution. and use dragAndDropBy(WebElement source, int xOffset, int yOffset) rather that clickAndHold().moveByOffset(x-offset, y-offset).release() To get actual offset manually you can use browser ruler.

0

It works :D

Actions builder = new Actions(driver);
builder.clickAndHold(driver.findElement(element)).build().perform();
builder.moveToElement(svgGridElement,50,50).build().perform();
builder.release().build().perform();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.