I have tried to search a text in google images and then drag the element and drop. However it is not performing the same. When we manually move the mouse some action is occurring but drag and drop function is not performed.
WebDriver driver= new FirefoxDriver();
driver.get("http://images.google.com/");
Thread.sleep(1000);
driver.findElement(By.name("q")).sendKeys("Apple");
driver.findElement(By.name("btnG")).click();
WebElement drag= driver.findElement(By.cssSelector("html body#gsr.srp div#main div#cnt.mdm div#rcnt div.col div#center_col div#res.med div#topstuff div#ifbc.prc div#ifb.prs a.rg_fbl div.rg_bb div.rg_bb_i div.rg_bb_layout div.rg_di img.rg_i"));
WebElement drop= driver.findElement(By.cssSelector("html body#gsr.srp div#tphdr.tphdr div#mngb div#gb.gb_1b div.gb_Ab div.gb_k div#gbq div#gbq2.gbt div#gbqfw.gbqfr form#gbqf.gb_Nb fieldset#gbqff.gbqff div#gbfwa.gbqfwa div#gbqfqw.gbqfqw div#gbqfqwb.gbqfqwc table#gs_id0.gstl_0 tbody tr td#gs_tti0.gsib_a div#gs_lc0 input#gs_htif0.gbqfif"));
Actions builder= new Actions(driver);
Action dragelement= builder.clickAndHold(drag).build();
dragelement.perform();
Action dropelement=builder.moveToElement(drop).release(drop).build();
dropelement.perform();
code to drag this element to an offset is
WebElement draggable = browser.findElement(By.id("draggable")); new
Actions(browser).dragAndDropBy(draggable, 200, 10).build().perform();
Try This...
WebDriver driver = new FirefoxDriver();
driver.get("http://images.google.com/");
driver.switchTo().frame(0);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
WebElement dragElement = driver.findElement(By.id("draggable"));
WebElement dropElement = driver.findElement(By.id("droppable"));
Actions builder = new Actions(driver); // Configure the Action
Action dragAndDrop = builder.clickAndHold(dragElement)
.moveToElement(dropElement)
.release(dropElement).build(); // Get the action
dragAndDrop.perform(); // Execute the Action
Related
I have performed drag and drop on unlayer web site and all positive combination I have used. but drap and drop not working.
We have tried following code with selenium 4.
#Test()
public void test() throws InterruptedException {
driver.get("https://dashboard.unlayer.com/create/blank?_gl=1*afclpx*_ga*NzU0NTMwNzU3LjE2NzYzMTAyOTk.*_ga_VMP9QH8KW8*MTY3NjMxMDI5OC4xLjEuMTY3NjMxMDMwOS41OC4wLjA.");
Thread.sleep(9000);
driver.switchTo().frame(0);
Thread.sleep(9000);
//driver.findElement(By.xpath("//div [text ()='Text']"));
WebElement source = driver.findElement(By.xpath("//div [text ()='Text']"));
//source.click();
Thread.sleep(9000);
WebElement target = driver.findElement(By.xpath("//div[text()='No content here. Drag content from right.']"));
System.out.println("target");
System.out.println("target= "+target.getText());
Thread.sleep(5000);
Actions a = new Actions(driver);
Thread.sleep(5000);
//a.clickAndHold(source).moveToElement(target).release().build().perform();
//a.clickAndHold(target);
//a.dragAndDrop(source, target);
//a.moveToElement(target);
a.click(source);
System.out.println("Click on="+"source");
Thread.sleep(5000);
a.release(target);
a.click(target);
System.out.println("Click on="+"target");
}
The desired element is within an <iframe>. So to perform Drag and Drop you have to:
Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
You can use either of the following Locator Strategies: solutions:
Using dragAndDrop() method:
driver.get("https://dashboard.unlayer.com/create/blank?_gl=1afclpx_gaNzU0NTMwNzU3LjE2NzYzMTAyOTk._ga_VMP9QH8KW8*MTY3NjMxMDI5OC4xLjEuMTY3NjMxMDMwOS41OC4wLjA.");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[data-tid='banner-accept']"))).click();
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[src*='https://editor.unlayer.com']")));
Thread.sleep(10);
WebElement dragMe = driver.findElement(By.xpath("//div[#class='blockbuilder-content-tool' and #draggable='true']//div[text()='Text']"));
WebElement dropHere = driver.findElement(By.xpath("//div[text()='No content here. Drag content from right.']"));
Actions actions = new Actions(driver);
actions.dragAndDrop(dragMe, dropHere).build().perform();
Using clickAndHold().moveToElement().release() methods:
driver.get("https://dashboard.unlayer.com/create/blank?_gl=1afclpx_gaNzU0NTMwNzU3LjE2NzYzMTAyOTk._ga_VMP9QH8KW8*MTY3NjMxMDI5OC4xLjEuMTY3NjMxMDMwOS41OC4wLjA.");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[data-tid='banner-accept']"))).click();
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[src*='https://editor.unlayer.com']")));
Thread.sleep(10);
WebElement drag = driver.findElement(By.xpath("//div[#class='blockbuilder-content-tool' and #draggable='true']//div[text()='Text']"));
WebElement drop = driver.findElement(By.xpath("//div[text()='No content here. Drag content from right.']"));
Actions actions = new Actions(driver);
actions.clickAndHold(drag).moveToElement(drop).release(drop).build().perform();
How many ways can we implement Drag & Drop functionality using Selenium and Java?
Code attempt:
Thread.sleep(3000);
WebElement FROM = driver.findElement(By.xpath("(//div[#class='item-container flex-container-horizontal'])[1]"));
Thread.sleep(3000);
WebElement to = driver.findElement(By.xpath("//div[text()='Product Quality?']"));
Thread.sleep(3000);
Actions act=new Actions(driver);
act.dragAndDrop(FROM, to).build().perform();
Drag and Drop functionality can be implemented in multiple ways as follows:
Using dragAndDrop():
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://jqueryui.com/droppable/");
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[#class='demo-frame']")));
WebElement from = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("draggable")));
WebElement to = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("droppable")));
new Actions(driver).dragAndDrop(from, to).build().perform();
Chaining clickAndHold() and moveToElement():
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://jqueryui.com/droppable/");
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[#class='demo-frame']")));
WebElement drag = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("draggable")));
WebElement drop = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("droppable")));
new Actions(driver).clickAndHold(from).moveToElement(to).release(from).build().perform();
Video demonstration:
I'm trying to click in some JQUERY elements from a very known website to practice Selenium (http://the-internet.herokuapp.com/jqueryui/menu).
I figured out how to navigate into the menu (not sure if my code is a good solution), however am not being able to click in each last submenu option (PDF, CSV, Excel)
I'm trying something like below:
Actions builder = new Actions(driver);
Action mouseOverMenu;
mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-2"))).build();
mouseOverMenu.perform(); //accessing Enabled menu option
mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-4"))).build();
mouseOverMenu.perform(); //accessing Downloads submenu option
String jQuerySelector = "$('a#ui-id-6.ui-corner-all')";
WebElement webElement = (WebElement) ((JavascriptExecutor) driver).executeScript("return $(" + jQuerySelector+ ").get(0);");
//click() also did not work
WebElement webElement = (WebElement) ((JavascriptExecutor) driver).executeScript("return $(" + jQuerySelector+ ").click();");
Your JavaScript function of click is wrong.
Use below javascript syntax
executor.executeScript("arguments[0].click();", WebElement);
Below code works for me:
Actions builder = new Actions(driver);
Action mouseOverMenu;
mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-2"))).build();
mouseOverMenu.perform(); //accessing Enabled menu option
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ui-id-4")));
wait.until(ExpectedConditions.elementToBeClickable(By.id("ui-id-4")));
mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-4"))).build();
mouseOverMenu.perform(); //accessing Downloads submenu option
WebElement webElement2= driver.findElement(By.cssSelector("a#ui-id-6.ui-corner-all")); // #ui-id-6 is for pdf, #ui-id-7 csv so on
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", webElement2);
I am trying to perform drag and drop with Selenium and Java and it is not working.. What can be the cause.. It doesn't give me any error but it is just not happening..
Here is my code.
public class ActionDragDrop {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.get("https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement f=driver.findElement(By.xpath("//*[#id='iframeResult']"));
driver.switchTo().frame(f);
WebElement drag = driver.findElement(By.xpath("//*[#id='drag1']"));
WebElement drop = driver.findElement(By.xpath("//*[#id='div1']"));
Actions builder = new Actions(driver);
Actions dragAndDrop = builder.clickAndHold(drag);
builder.moveToElement(drop);
builder.release(drop);
builder.build();
dragAndDrop.perform();
}
}
You will to switch to iframe first in order to perform drag and drop event:
driver.switchTo().frame(0); //Move inside to the frame.
WebElement body = driver.findElement(By.tagName("body"));
body.click();
WebElement from = driver.findElement(By.xpath("//your xpath"));
Actions act = new Actions(driver);
act.clickAndHold(from).build().perform();
Thread.sleep(4000);
driver.switchTo().defaultContent(); //Move outside to the frame.
driver.switchTo().frame(1); //Move inside to another frame.
WebElement body = driver.findElement(By.tagName("body"));
body.click();
WebElement to = driver.findElement(By.id("guide_RIGHT_SAFETY_rect"));
act.clickAndHold(to).moveToElement(to).release(to).build().perform();
Thread.sleep(2000);
driver.switchTo().defaultContent(); //Move outside to another frame.
Note : Kindly use your xpath, id, classname etc, I have just copied an example. More or less idea should be same.
Try this below code.
As your from and to webelement is located inside the same iframe. First you need to switch inside the iframe.
driver.get("https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop");
driver.manage().window().maximize();
driver.switchTo().frame("iframeResult"); //Move inside to the frame.
WebElement body = driver.findElement(By.tagName("body"));
body.click();
WebElement from = driver.findElement(By.xpath("//img[#id='drag1']"));
WebElement to = driver.findElement(By.xpath("//div[#id='div1']"));
Actions act = new Actions(driver);
act.clickAndHold(from).perform();
Thread.sleep(4000);
act.clickAndHold().moveToElement(to).release(to).build().perform();
Thread.sleep(2000);
driver.switchTo().defaultContent(); //Move outside to the frame.
I have done many experiments and finally found the solution as below code in Python.
The failure at DragAndDrop is not caused by iframe.
Just separate every step and perform it.
# drag leftbox and drop on rightbox
actions = ActionChains(driver)
actions.click_and_hold(leftbox).perform()
sleep(4)
actions.move_to_element(rightbox).perform()
sleep(4)
actions.release(rightbox).perform()
I'm using the following to navigate a menu in Selenium. It works perfectly in Chrome, however in IE, it ends up clicking the menu below my target and the submenu item becomes completely inaccessible.
// Actions not supported by FireFox's Marionette Driver, use chrome or ie.
Actions actions = new Actions(driver);
WebElement menuHoverLink = driver.findElement(By.id("m7f8f3e49_ns_menu_INVENTOR_MODULE_a_tnode"));
System.out.println("Found the inventory text");
actions.moveToElement(menuHoverLink);
WebElement subLink = driver.findElement(By.id("m7f8f3e49_ns_menu_INVENTOR_MODULE_sub_changeapp_INVENTOR_a"));
actions.moveToElement(subLink);
actions.click();
actions.perform();
And here is where the drive is initialized
System.setProperty("webdriver.ie.driver", "C:\\Selenium\\IEDriverServer64.exe");
driver = new InternetExplorerDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
I would like to be able to click on the proper menu item to continue with my testing.
I managed to get a working solution by moving the cursor over from the offset. Not sure how well this will work across screens.
Actions actions = new Actions(driver);
WebElement menuHoverLink = driver.findElement(By.linkText(arg1));
//<span id="m7f8f3e49_ns_menu_INVENTOR_MODULE_a_tnode">Inventory</span>
System.out.println("Found the inventory text");
actions.moveToElement(menuHoverLink);
actions.moveByOffset(100, 10);
Thread.sleep(1000);
//actions.moveByOffset(45, 0);
WebElement subLink = driver.findElement(By.
id("m7f8f3e49_ns_menu_INVENTOR_MODULE_sub_changeapp_SRVITEM_a_tnode"));
actions.moveToElement(subLink, 100, 12)
.click().perform();