Right Click Drag and Drop Selenium Webdriver - selenium-webdriver

so I'm trying to perform a right click drag and drop using Selenium webdriver, I was wondering if anyone had any ideas on how to do this?
I have tried using ActionChains to do it, but they dont seem to run.

For right click with mouse you can use
WebElement elementToRightClick = driver.findElement(By.id("gbqfba"));
Actions clicker = new Actions(driver);
clicker.contextClick(elementToRightClick).perform();
For drag&drop, you can use
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(source Element).moveToElement(target Element).release(target Element).build();
dragAndDrop.perform();
(or)
Actions builder = new Actions(driver);
Action dragAndDrop = builder.dragAndDrop(source Element, target Element).build();
dragAndDrop.perform();

Related

Hover Electronics -> Mobiles and Click using Selenium in Flipkart UI using Chrome

Actions action = new Actions(driver);
WebElement we = driver.findElement(By.xpath("//*[#id=\"container\"]/div/div[2]/div/ul/li[1]/span"));
action.moveToElement(we).moveToElement(driver.findElement(By.xpath("//*[#id=\"container\"]/div/div[2]/div/ul/li[1]/ul/li/ul/li[1]/ul/li[1]/a[#href='/mobile-phones-store?otracker=nmenu_sub_Electronics_0_Mobiles']"))).click().build().perform();
I am trying to hover and click using xpath. The code is not throwing any error but it is still unable to perform click option after hover.
Try with the below code snippet. I have tested it with chrome driver and works fine.
WebElement electronics_menuname = driver.findElement(By.xpath("//span[contains(text(),'Electronics')]"));
Actions builder = new Actions(driver);
builder.moveToElement(electronics_menuname).build().perform();
WebElement mobile_menu_button = driver.findElement(By.xpath("//li[#class='_1KCOnI _2BfSTw _1h5QLb _3ZgIXy']//a[contains(text(),'Mobiles')]"));
new WebDriverWait(driver,20).until(ExpectedConditions.elementToBeClickable(mobile_menu_button)).click();

Selenium webdriver (Double click on a search result displayed as label)

I am very new to selenium and need to double click on a label(appears after a successful search result).
Please help.
enter image description here
Attached is the page look like.
You should use the Actions() class as this includes a 'double-click' action.
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//select[#id='groupselect']/#option[#value='942711']"))).doubleClick().build().perform();
OR
Actions action = new Actions(driver);
WebElement element=By.xpath("//select[#id='groupselect']/#option[#value='942711']"));
//Double click
action.doubleClick(element).perform();
//Mouse over
action.moveToElement(element).perform();
//Right Click
action.contextClick(element).perform();
Hope it will help you :)

Selenium webdriver automation, automating context menu options

I am not able to click on any element after doing right click in selenium(Java).
Its just doing a right click and is not clicking of any of the options like open in new tab...Instead its just doing a normal click after doing a right click.Can anyone please help me.Below is my code
System.setProperty("webdriver.chrome.driver","C:\Selenium\chromedriver.exe");
WebDriver wd=new ChromeDriver();
wd.get("http://google.com");
Thread.sleep(3000);
//Point a=wd.findElement(By.linkText("Testing")).getLocation();
WebElement b=wd.findElement(By.linkText("About"));
Actions action=new Actions(wd);
//action.contextClick(b).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).perform();
action.moveToElement(b);
Thread.sleep(4000);
//action.contextClick(b);
action.contextClick(b);
action.sendKeys(Keys.ARROW_DOWN).sendKe ys(Keys.ENTER).build().perform();
I have tried via context click and move to element as well but no result.Thanks in advance..
Might be this is what you want:
To select the item from the contextual menu, you have to just move your mouse positions with the use of Key down event like this:-
Actions action= new Actions(driver);
action.contextClick(b).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
Soure: Select an Option from the Right-Click Menu in Selenium Webdriver - Java
As per request in the comments adding alternative way to open a link in new tab.
System.setProperty("webdriver.chrome.driver", "Drivers\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("http://www.google.com/");
WebDriverWait wait = new WebDriverWait(driver, 30);
Thread.sleep(3000);
WebElement b=driver.findElement(By.linkText("About"));
Actions action=new Actions(driver);
action.moveToElement(b).perform();
Thread.sleep(4000);
action.keyDown(Keys.CONTROL);
action.click();
action.keyDown(Keys.CONTROL).build().perform();
//action.sendKeys(Keys.RETURN).perform();
Thread.sleep(4000);
driver.quit();
The build() method is used compile all the listed actions into a single step. We use build() when we are performing sequence of operations. We can directly use perform() if we are performing single action. You can read more about Actions class.
Also, re-iterating that I was unable to find the root of the issue in the limited time I had, so I placed a work around. So like we use the shortcut CTRL+CLICK to open the link in new tab manually, You might need to find out the shortcut for what you need, you can refer this : https://support.google.com/chrome/answer/157179?hl=en
Hope it helps :)

Selenium Webdriver drag and drop NOT working in Chrome

I used the below code for drag and drop. It worked in Firefoxdriver but NOT in chromedriver.
WebElement dragElement = driver.findElement(By.id(dragid1));
WebElement dropElement = driver.findElement(By.id(dropid1));
Actions builder = new Actions(driver);
Action drag = builder.clickAndHold(dragElement).build();
drag.perform();
Action move = builder.moveByOffset(355, -20).build();
move.perform();
TimeUnit.SECONDS.sleep(2);
Actions release = builder.clickAndHold(dropElement).release();
release.perform();
Please Help!
If you have both source and target IDs, then why don't you try to use drag and drop?
I'm not very good with Java, but here's how I've done it in Python. I hope it helps you a bit.
from selenium.webdriver.common.action_chains import ActionChains
actionChains = ActionChains(driver)
actionChains.drag_and_drop(dragElement, dropElement).perform()
Tried below sample code with chromedriver:2.15, chrome:v43 and is working fine with Chrome.
Sample Code:
System.setProperty("webdriver.chrome.driver","drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(1,TimeUnit.MINUTES);
driver.get("http://jqueryui.com/droppable");
driver.switchTo().frame(0);
WebElement dragElement = driver.findElement(By.id("draggable"));
WebElement dropElement = driver.findElement(By.id("droppable"));
Actions builder = new Actions(driver);
builder.clickAndHold(dragElement).moveToElement(dropElement).release().build().perform();
Try bundling all those seperate Action objects into a single Actions object
Actions act = new Actions(driver);
act.ClickAndHold(dragElement );
act.MoveToElement(dropElement );
act.Release(dragElement );
act.Build().Perform();
Note: For me, in Chrome & IE, sometimes just dragging to an Element was not enough to make it stick there, and I would have to add in an extra act.MoveByOffset(0, 5); before releasing to move just a few pixels, which seems to work
Is there a reason you have to wait for 2 seconds before releasing, or is that just what worked in FF?
I had the same problem but got to override it like this:
//Setup robot
Robot robot = new Robot();
robot.setAutoDelay(50);
//Maximized browser:
robot.keyPress(KeyEvent.VK_F11);
Thread.sleep(2000);
WebElement dragElement = driver.findElement(drag_element);
Actions builder = new Actions(driver);
builder.dragAndDropBy(dragElement,0, 200).build().perform();

Is drag-and-drop possible in chrome-webdriver?

I cannot get drag-and drop working with the Java Junit WebDriver bindings. I am working with Google Chrome on Windows.I have used below code,but i did not work.
{WebElement draggable = driver.findElement(By.id("source"));
WebElement to = driver.findElement(By.id("target"));
Actions builder = new Actions(driver);
builder.dragAndDrop(element, to).build().perform();
builder.clickAndHold(element).moveToElement(to).release(to).build().perform();}
Please try the following solution (it works for us):
WebElement dragArea = driver.findElement(By.cssSelector("#" + idDragProduct"));
WebElement to = driver.findElement(By.cssSelector("#" + idTarget));
new Actions(driver).clickAndHold(dragArea).moveToElement(to).release().build().perform();
In case you want to try and test drag-and-drop as functionality, it's not neccesary to move it to another element.
You can do:
WebElement to = driver.findElement(By.cssSelector("#" + idTarget));
new Actions(driver).clickAndHold(dragArea).moveByOffset(X_OFF,Y_OFF).release().build().perform();

Resources