How to use clickAndHold method of Actions class? - selenium

I encountered the following code:
List<WebElement> listItems = driver.findElements(By.xpath("//*[#id='select_items']/li"));
action.clickAndHold(listItems.get(1)).clickAndHold(listItems.get(2)).click().build().perform();
I can't understand why there is call to method click
If we already chose item1 and item2 by clickAndHold method, what is the purpose of calling also to click method?

I think that the click() is needed to release the pressed mouse button.
Alternative (I got it from https://www.selenium.dev/documentation/webdriver/actions_api/mouse/)
Actions actionProvider = new Actions(driver);
actionProvider.clickAndHold(sourceEle).moveToElement(targetEle).build().perform();
// Performs release event
actionProvider.release().build().perform();

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();

Use of Actions class in Selenium Webdriver

I was going through the selenium learning and when I was exploring Handling Windows and frames topic in the text, I found this code. Is it necessary to instantiate the Actions class and why we need to do that if we have never going to use it in the code?
public class SwitchBetweenFrames {
public static void main(String... args) {
WebDriver driver = new FirefoxDriver();
driver.get("file://C:/Frames.html");
Actions action = new Actions(driver);
driver.switchTo().frame(0);
WebElement txt = driver.findElement(By.name("1"));
txt.sendKeys("I'm Frame One");
driver.switchTo().defaultContent();
driver.switchTo().frame(1);
txt = driver.findElement(By.name("2"));
txt.sendKeys("I'm Frame Two");
}
}
No, we do not need the Action if nor required. Switching to frame or alert is different and using Action is different concept.
You can use different functions of Action class
http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/interactions/Actions.html
And for switch is only used to shift control of webdriver from one page to another or one frame to another
No, it isn't a necessary step to instantiate Actions class, unless there is an actual need of it. For your code, it's not needed at all. Your code will still work, if you comment that section out.
Usually, Actions class can be used to emulate some user gestures, like: drag and drop, click and hold, etc., that by normal standards, is a tad tricky to implement.
No, it is not necessary to user Action class for passing control to a window. For Frames we are just passing the control to another frame. So SwitchTo is used.
Action classes are commonly used for mouse hover actions, drag and drop, click and hold etc like that.
If you want to know more about action class, check the link,
Actions class

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 can you do right click using selenium?

im trying to preform a right click using selenium, any thoughts on how to do this?
According to the OpenQA.Selenium.Interactions Namespace.
// step 1 - select the element you want to right-click
var elementToRightClick = this.Driver.FindElement(By.Id("elementtoclickonhasthisid"));
// step 2 - create and step up an Actions object with your driver
var action = new OpenQA.Selenium.Interactions.Actions(this.Driver);
action.ContextClick(elementToRightClick);
// step 3 - execute the action
action.Perform();
Please see docroots's answer for selenium.
To generally simulate a right click in JavaScript, have a look at JavaScript simulate right click through code.
it appears that for my issue (an element that opens a popup after a right click), using selenium's : mouse_down_right() and then mouse_up_right()
worked as well. thanks.
Selenium is offering a method for right click - ContextClick:
public void RightClick(IWebElement target)
{
var builder = new Actions(driver);
builder.ContextClick(target);
builder.Perform();
}
I've tried ActionSequence and it worked.
ContextClick function is not found, you should use click.
So, it should be as follows:
driver.actions().click(element,2).perform();
The element is your web element, 2 means right click.