Selenium: move Mouse - selenium

Task is to move physical cursor to the element.
Driver correctly finds element on the page.
WebElement label = driver.findElement(By.xpath("//form[#id='loginForm']")).
Move cursor does not work.
Tried following:
Actions act = new Actions(driver);
act.moveToElement(label, 1, 1).click().build().perform();
And following:
Actions act = new Actions(getDriver());
act.moveToElement(label).build().perform();

Try this.
driver.findElement(By.xpath("//form[#id='loginForm']")).equals(driver.switchTo().activeElement())

Solution was to use Robot class and use there - mouseMove

Related

Selenium: Click and Hold the element and then Slide it

In my application I have a scenario where I need to slide an element in list view to add new entry to the item. I have to automate it using Selenium and C#. Application is developed using Ionic and Angular Frameworks.
In Selenium there is an option to ClickAndHold and MoveByOffset methods but none of these seems to be working. At the same time no errors are displayed. Please help.
Code I have tried so far is as below.
Actions dragger = new Actions(driver);
elementToSlide = driver.FindElement(By.XPath("//ion-item-slide[1]"));
dragger.ClickAndHold(elementToSlide).MoveByOffset(-47,0).Build().Perform();
Images are attached for reference. The first element in the list view slides.
Maybe the drag and drop command could solve your problem,try like this
IWebElement elementToSlide = driver.FindElement(By.XPath("//ion-item-slide[1]"))
Actions dragger = new Actions(driver);
dragger.DragAndDropToOffset(elementToSlide, -47, 0).Build().Perform();
I had the same problem, I wrote the code like this and it worked for me:
Actions action = new Actions(driver);
action.clickAndHold(elementToSlide);
action.moveToElement(NextplaceElement).release();
action.build().perform();
Thanks for your answers, actually I figured out in ionic framework we should not use ion-item-sliding element for sliding, but the next child which is ion-item. I did by using the following code.
var slidableItm = driver.FindElement(By.XPath("//ion-item-sliding/ion-item");
Actions dragger = new Actions(driver);
dragger.ClickAndHold(slidableItm))).MoveByOffset(-47, 0).Build().Perform();
Then after sliding we need to click on some element's coordinates to complete and release the slide. It can be achieved by following code.
var clickableItm = driver.FindElement(By.XPath("/ion-item-sliding/ion-item/div[1]/div/ion-label/div/p[1]"));
ILocatable c = (ILocatable)clickableItm;
RemoteWebDriver rd = (RemoteWebDriver)driver;
rd.Mouse.Click(c.Coordinates);
Actions a = new Actions(driver);
a.clickAndHold(e2).pause(2000).moveToElement(e1).release().build().perform();

Click on the element which is visible after mouse hovering

I have to click on a tile which is generated after mouse hovering. I wrote the code below but it is still not working.
WebElement FrontElement=driver.findElement(By.xpath("//a[#class='sol-itm-bx relative front-app-nm']/span[text()='UI Auto Test12345']"));
WebElement BackElement= driver.findElement(By.xpath("//a[#class='relative back-app-desc']/span[text()='UI Auto Test12345']"));
Actions builder = new Actions(driver);
builder.moveToElement(FrontElement);
builder.perform();
builder.clickAndHold(FrontElement);
BackElement.click();
To use the Actions() class you need to chain the actions together. Separate commands won't work in the way you want.
Actions builder = new Actions(driver);
builder.moveToElement(driver.findElement(By.xpath("//a[#class='sol-itm-bx relative front-app-nm']/span[text()='UI Auto Test12345']")))
.moveToElement(driver.findElement(By.xpath("//a[#class='relative back-app-desc']/span[text()='UI Auto Test12345']")))
.click().perform();
Note: I've separated the lines for ease of reading.
EDIT: 'build' to 'builder' NullPointerException

program stuck using drag and drop selenium

I am trying to move slider using drag and drop. It identifies the element and clicked on it and after that nothing happens and my code stuck there itself(like waiting for user input). As soon as i moved my mouse little bit manually it executes rest of the code and works as expected. please help me what is this weird behavior.?. below is the code i used to build drag and drop.
Actions builder = new Actions(driver);
Action secondSlide = builder.dragAndDropBy(secondSlider, 50, 0).click().build();
System.out.println("waiting");
secondSlide.perform();
System.out.println("not waiting");
"Waiting" message is printing nicely but it doesn't print "not waiting" as it stuck in "secondSlide.perform()" line. But as soon as i moves my mouse little bit manually it prints "not waiting" and program ends gracefully.
Try to do it differently. Here is a number of approaches:
WebElement element = driver.findElement(By.name("element dom name"));
WebElement target = driver.findElement(By.name("target dom name"));
(new Actions(driver)).dragAndDrop(element, target).perform();
Or:
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(someElement)
.moveToElement(otherElement)
.release(otherElement)
.build();
dragAndDrop.perform();

How to send right and left cursor using Soda/Selenium?

I'm using Soda to run Selenium Webdriver. Mostly it's working as expected but I'm trying to figure how to send the right and left cursor keys to the browser to move a jquery ui slider handle.
I tried
.typeKeys('css=a.ui-slider-handle[lr="l"]','\37')
and
.type('css=a.ui-slider-handle[lr="l"]','\37')
and
.typeKeys('\37')
and
.type('\37')
Nothing seems to move the slider. None of them error either. I'm sending a click to the handle before I do this just to be sure...
Anyone know how to do this?
Working code in Java-
WebDriver driver = new InternetExplorerDriver();
driver.get("http://jqueryui.com/demos/slider/");
//Identify WebElement
WebElement slider = driver.findElement(By.xpath("//div[#id='slider']/a"));
//Using Action Class
Actions move = new Actions(driver);
Action action = move.dragAndDropBy(slider, 30, 0).build();
action.perform();
driver.quit();
Source - https://gist.github.com/2497551
Try below, I tested this in firefox with jquery UI slider page and it worked for me.
.clickAt("//div[#id='slider']/a[1]", "")
//mouse left key down
.mouseDownAt("//div[#id='slider']/a[1]", "0,0")
//move the cursor some 200 from left
.mouseMoveAt("//div[#id='slider']", "200,0")
//Release the mouse button
.mouseUpAt("//div[#id='slider']", "");

How to perform drag and drop using selenium-webdriver when target and destination element are in different frames?

I have to drag an image and drop it into a CQ5 component. The image and component are in different frames.
Here is the code which did not work as webelement destinationcould not be found when the target's frame was active.
new Actions(driver).dragAndDrop(target, destination).perform();
I have also tried to switch frame in between action as:
Actions builder = new Actions(driver);
Actions action = builder.clickAndHold(target);
driver.switchTo().frame("newFrame"); //switching frames
builder.moveToElement(destination);
builder.release(destination);
builder.build();
action.perform();
This is did not work either. Then, I tried moving the image by offset
new Actions(driver).dragAndDropBy(target, x, y).perform(); // x and y
This moved the image but component did not capture it, probably becuase action was too fast. Is there any way such drag drop can be done?
Thanks in advance.
You need to break it into two parts.
// grab your element
Actions builder = new Actions(driver);
Actions action = builder.clickAndHold(target);
builder.build();
action.perform();
// switch to the frame (you havent told webdriver to un-grab
driver.switchTo().frame("newFrame"); //switching frames
// move and drop
Actions builder = new Actions(driver);
Actions action = builder.moveToElement(destination);
builder.release(destination);
builder.build();
action.perform();
It seems there are some issues with selenium / webdriver drag and drop. I have submitted a defect with selenium folks, http://code.google.com/p/selenium/issues/detail?id=4420
Hopefully we will get some positive response.
I had the same prb as you. I can't darg and drop two elements from one frame to anther.
the ansers uppers are correct but from selenium 3 this solution doesn't works any more.
The workarrown is to move source element (after clickAndHol) to the positon 0,0 and then to move it under the second frame. for example 150,150.
Actions builder = new Actions(driver);
// switsh to the source frame
driver.switchTo().frame("sourceFrame");
// take the element with mouse
builder.clickAndHold(sourceElt).build().perfom();
// move mouse to the top of the source frame
builder.moveToElement(sourceElt, 0, 0 ).build().perfom();
// move the mouse under the target frame (specific to your case)
builder.moveToElement(sourceElt, 150,200).build().perfom();
// switsh to the target frame
driver.switchTo().frame("targetFrame");
builder.moveToElement(targetElt).build().perform();
builder.release(target).build().perform();
hope I helps you too.
Is anybody found solution for Adobe CQ 5.5?
I'm facing to the same problem with adobe CQ 5.5, I was trying multiple different ways, I can get image to drop zone, but once it is there the image still seems not active and dropping it not make sense. I figured out that it is because the mouse pointer is not moving with image that is why dropping not make sense. I added code to move mouse to the drop zone, but looks like commands is working separate so still not able to drop, please any suggestion.
Here is my code (not working on CQ 5.5)
String handle = driver.getWindowHandle(); // for main window
// Switch to Window to be able select image
driver.switchTo().window(handle);
WebElement dragble = driver.findElement(By.xpath("//xpath"));
Actions builder = new Actions(driver);
builder.clickAndHold(dragble);
Action action2 = builder.build();
action2.perform();
// Then, switch to iframe
driver.switchTo().frame("cq-cf-frame");
WebElement droppable = driver.findElement(By.cssSelector("#cssSelector of droppable"));
// Robot to point mouse to droppable zone
Point coordinates = driver.findElement(By.cssSelector("#cssSelector of droppable")).getLocation();
Robot robot = new Robot();
// Find location for droppable element
int x = driver.findElement(By.cssSelector("#ext-comp-1271")).getLocation().getX();
int y = driver.findElement(By.cssSelector("#ext-comp-1271")).getLocation().getY();
// Move dragble to droppable
builder = new Actions(driver);
builder.moveByOffset(x,y).perform().
builder.build();
builder.release();
robot.mouseMove(coordinates.getX(),coordinates.getY()+120);
builder.release(droppable).perform();
The above posted solutions did not work for me under CQ 5.5 and CQ 5.6
This works:
Actions builder = new Actions(driver);
builder.clickAndHold(sideKickComponent);
Action action = builder.build();
action.perform();
driver.switchTo().frame("cq-cf-frame");
builder = new Actions(driver);
builder.moveToElement(destination).perform();
builder.build();
builder.release();
builder.release(destination).perform();
This method allows convenient component placement:
public void addComponentByDragAndDrop(String sideKickComponentName, WebElement destination){
driver.switchTo().defaultContent();
WebElement sidekick = driver.findElement(By.id("cq-sk"));
List<WebElement> components =sidekick.findElements(By.tagName("button"));
WebElement sideKickComponent = null;
for (WebElement webElement : components) {
if (webElement.getText().equals(sideKickComponentName)) {
sideKickComponent = webElement;
break;
}
}
if (sideKickComponent == null) {
fail("SideKick component with the name: "+sideKickComponentName + " was not found.");
}
Actions builder = new Actions(driver);
builder.clickAndHold(sideKickComponent);
Action action = builder.build();
action.perform();
driver.switchTo().frame(Consts.CQ_MAIN_FRAME);
builder = new Actions(driver);
builder.moveToElement(destination).perform();
builder.build();
builder.release();
builder.release(destination).perform();
}
The code below works, hope it helps:
WebElement dragElement = (WebElement) elements.get(sourceElement);
Actions builder = new Actions(driver);
Actions action = builder.clickAndHold(dragElement);
action.build().perform();
driver.switchTo().frame("cq-cf-frame");
WebElement dropElement = driver.findElement(By.id("ext-comp-1411"));
builder.moveToElement(dropElement).build().perform();
//click the destination
builder.click(dropElement).build().perform();
//back to main page to release the hold mouse
driver.switchTo().defaultContent();
builder.release(dragElement).build().perform();
To do a drag and drop from an iframe to another, you need to reference all your actions in the iframe of the source web element. To do this, you should get the parent of the target iframe and manipulate using it, i.e. CqFrameParent which is a div that has the target iframe.
Since the source and target belongs to a single iframe, no need to do the switching of iframes to do this.
builder.moveElement(CqFrameParent(), targetX, targetY).build().perform();
builder.release().build().perform();
Create object of actions class
Actions act=new Actions(driver);
Find element xpath which we need to drag
WebElement drag=driver.findElement(By.xpath("put x path"));
Find element xpath where we need to drop
WebElement drop=driver.findElement(By.xpath("put x path"));
Drag element to destination
act.dragAndDrop(drag, drop).build().perform();
To use drag and drop in CQ use double click function first and put any component then try above method.
This code works on CQ 5.5
driver.switchTo().defaultContent();
Actions builder = new Actions(driver);
builder.clickAndHold(target);
Action action = builder.build();
action.perform();
driver.switchTo().frame("cq-cf-frame");
builder.moveToElement(destination);
builder.release(destination);
action = builder.build();
action.perform();
String source = "xpath_of_source";
String destination = "xpath_of_destination";
// grab your element
Actions builder = new Actions(driver);
Actions action = builder.clickAndHold(driver.findElement(By.xpath(source)));
builder.build();
action.perform();
// switch to the frame
driver.switchTo().frame("newFrame"); //switching frames
// move and drop
builder = new Actions(driver);
action = builder.moveToElement(driver.findElement(By.xpath(destination)));
builder.release(driver.findElement(By.xpath(destination)));
builder.build();
action.perform();
Selenium webdriver is providing drag and drop function. Try this
WebElement element = driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));
(new Actions(driver)).dragAndDrop(element, target).perform();