I know there are several posts about this issue. Tried almost all the solutions in my project but unable to make drag and drop work. I am using following code:
WebDriver driver = new ChromeDriver();
WebElement dragableElement =
driver.findElement(By.className("dragElement"));
WebElement dropableContainer = driver.findElement(By.xpath("//*
[#id='contentCollection_xyz']"));
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(dragableElement)
.moveToElement(dropableContainer)
.release(dropableContainer)
.build();
dragAndDrop.perform();
This drops my dragable element just before dropable container.
I have gotten drag and drop to work with selenium, this is a poorly worded question though. We need more details, and stack overflow is not the place to find someone to "share your screen with".
Related
I want to automate a scenario using selenium, java, appium and android for tapping on an element multiple times in a faster way then a button appears, I have tried through java code using for loop with selenium webdriver click/appium touchactions tap but nothing helped me.
If you want to perform multiple tap on the element.
Use the below code:
WebElement element=driver.findElement(By.id("someID"));
TouchAction actionOne = new TouchAction();
actionOne.tap(element);
actionOne.release();
TouchAction actionTwo = new TouchAction();
actionTwo.tap(element);
actionTwo.release();
MultiTouchAction action = new MultiTouchAction();
action.add(actionOne);
action.add(actionTwo);
action.perform();
Add as many taps as possible if you need more than two. This should work.
This is website: https://html5demos.com/drag/
And this is my code perform drag and drop element using java, chrome and selenium 3.141.59
By dustBin = By.xpath("//div[#id=\"bin\"]");
By one = By.xpath("//a[#id=\"one\"]");
WebElement From = driver.findElement(one);
WebElement To = driver.findElement(dustBin);
Actions actions = new Actions(driver);
actions.dragAndDrop(From,To).build().perform();
actions.clickAndHold(From).moveToElement(To).build().perform();
actions.dragAndDropBy(From,176,260).build().perform();
When I run test with TestNG the element is not moving to dustBin. So I dont know why. Any one help me resolve this issue. Thank you.
You should use findElementById to find locator instead of xpatch.
Try this one:
WebElement sourceLocator = driver.findElement(By.id("one"));
//To get target locator
WebElement targetLocator = driver.findElement(By.id("bin"));
I need to press control+mouse click keys using Selenium WebDriver(java). I need to select multiple element in my script.
Is there any way to do it?
I checked the Selenium libraries and found that selenium allows key press of special and functional keys only.
There is already written library Actions in WebDriver which you can use.
Short Description of what is happening:
First you are pressing the Control button and then you are clicking (in this case) 3 times on your defined WebElemen objects) then your are unpressing the Control and finish your Actions.
In this case you can achive the selection of 3 items (or opening a 3 new tabs) depending on what your WebElements are.
Actions actions = new Actions(driver);
actions.keyDown(Keys.LEFT_CONTROL)
.click(first_WebElement)
.click(second_WebElement)
.click(third_WebElement)
.keyUp(Keys.LEFT_CONTROL)
.build()
.perform();
Do it with the help of 'Actions' as below:
Actions action=new Actions(driver);
action.keyDown(Keys.CONTROL).build().perform();
driver.findElement(By.xpath(".//*[#id='selectable']/li[1]")).click();
driver.findElement(By.xpath(".//*[#id='selectable']/li[3]")).click();
action.keyUp(Keys.CONTROL).build().perform();
In the case of using Mac, te code would be next:
action.keyDown(Keys.COMMAND)
.click(WebElement)
.keyUp(Keys.COMMAND)
.build()
.perform();
You achieve same using jquery code
JavascriptExecutor js = (JavascriptExecutor) driver;
String script = "e = jQuery.Event('click');e.ctrlKey = true; $('secondRow_Css_locator').trigger(e);";
js.executeScript(script);
OR you can also use robot class but it can lock your screen for a moment
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
As of 2018 the is the first results pops up. Earlier it was working fine after FF 61 (direct jump form 47 to 61) It starts breaking. unfortunately none of the answer worked for me. Solved it by action.keyDown(Keys.CONTROL).click(myWebElements.get(i)).keyUp(Keys.CONTROL).perform(); just iterating every element one by one
it worked with me using this:
Actions action=new Actions(driver);
action.keyDown(Keys.CONTROL).build().perform();
driver.findElement(By.xpath(".//*[#id='selectable']/li[1]")).click();
driver.findElement(By.xpath(".//*[#id='selectable']/li[3]")).click();
action.keyUp(Keys.CONTROL).build().perform();
I'm trying to perform an action on a button it's never done.
final Actions action = new Actions(mDriver);
final WebElement myCart = mDriver.findElement(By.cssSelector("path to my span"]"));
final WebElement myButton = mDriver.findElement(By.cssSelector("path to my button"));
action.moveToElement(myCart).build().perform();
action.moveToElement(myButton).click().build().perform();
This code works perfectly with firefox but not with phantom JS
I found some issue here How to handle Mouseover in Selenium 2 API or How to perform mouseover function in Selenium WebDriver using Java? but nothing work with phantom.
Is there any known workaround for this ?
Thanks!
I had similar issues when I used GhostDriver and PhantomJS around a year ago (FYI article). Actually I had problems with IE_Driver and Chrome_Driver too, mostly related with visibility of elements outside the screen_frame (page must be scrolled down).
One of most serious issues was the upload_window and handle it through already-mentioned. I wasn't able to achieve it doh. But my workaround was to switch/cast the driver on these problematic places and after they complete/handle the operation - switch it back to GhostDriver. Even by doing so, the execution speed was impressive.
Hope this helps - even late given.
Update:
find IWebElement to process
set WebDriver from GhostDriver to FirefoxDriver
process the IWebElement item with current WebDriver as FirefoxDriver
verify expected result from processing the IWebElement item
set back WebDriver from FirefoxDriver to GhostDriver
continue workflow
As far as I remember my Test framework implementation - a BaseTest class takes care of initialization of used WebDriver and ISelenium objects. So for your more specific case, you can try this:
// Create a new instance of the Ghost driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new GhostDriver();
//do stuff until new driver is needed
driver = new FirefoxDriver();
//do stuff with new driver
//'cast' back after required operations have been completed and verified
driver = new GhostDriver();
How to write selenium java code for doubleClick() on a record using web driver?
I have displayed some records in the body part. Once I clicked on a record we should get a popup window to update it.
Please suggest how to write Selenium Java code using web driver.
I have tried following code:
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//table/tbody/tr[2]/td/div/div/table/tbody/tr[10]/td[1]"))).doubleClick().build().perform();
Use Actions class to perform mouse, keyboard actions on WebElements using WebDriver.
Actions action = new Actions(driver);
WebElement element=driver.findElement(By.linkText("TEST"));
//Double click
action.doubleClick(element).perform();
//Mouse over
action.moveToElement(element).perform();
//Right Click
action.contextClick(element).perform();
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//table/tbody/tr[2]/td/div/div/table/tbody/tr[10]/td[1]"))).doubleClick().perform();
This code works!!!
You should use the Actions() class as this includes a 'double-click' action.
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.linkText("Test"))).doubleClick().build().perform();
Try this code:
Actions action = new Actions(driver);
WebElement btnElement=driver.findElement("Locator of element"));
action.doubleClick(btnElement).build().perform();
You can make use of Actions class of WebDriver to perform composite actions like
Double click, Drag and Drop, Hover etc.
// Creates an instance of Actions class, passing current driver instance.
Actions builder = new Actions(driver);
Way 1:
// Gets an Action class object that holds an action/ a set of actions
Action action = builder.doubleClick(element);
// Builds the set of actions/ single action using build() and executes on browser using perform() method.
action.build().perform();
Way 2:
// Calls build() and perform() methods directly on Actions class instance
builder.doubleClick().build().perform();
And in case if have no additional actions binded to singleclick, you can use:
driver.findElement(By.xpath("%youXPath%"))).click;
driver.findElement(By.xpath("%youXPath%"))).click;
Actually, it should works in most cases (except you have some custom system doubleclick settings)
WebElement element = driver.findElement(selector);
Actions builder = new Actions(driver);
builder.doubleClick(element).perform();
I implemented Ran's (immediately above my post) solution. I'm writing Java in Eclipse and using Selenium WebDriver.
There are 2 imports that you'll need:
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
Then, I implemented the code thusly:
WebElement element = driver.findElement(By.xpath("/html/body/div[1]/div/div/div[2]/div[1]/div[3]/div[8]/div[2]/div/div[2]/div/table/tbody/tr[2]"));
Actions builder = new Actions(driver);
builder.doubleClick(element).perform();
Thanks to Ran!
I'd been struggling with this one for several hours. Invoking the single click twice doesn't work for me - too much time between the events to be captured by the browser under test as a double-click.