Selenium DragnDrop not working properly - selenium

I am trying to moving a div with Selenium DragnDrop. Here is my Code
WebElement fromElement= driver.findElement(By.xpath("layoutDivPosition_locator"));
WebElement toElement=driver.findElement(By.xpath("layoutDivPosition_locator"));
Actions action = new Actions(driver);
Action dragnDrop = action.clickAndHold(fromElement).moveToElement(toElement).release(toElement).build();
dragnDrop.perform();
Please help for this .

Selenium's actions for drag and drop do not play nice with HTML5, when you trying to move element to some frame and in several other situations. Possibly you can solve your problem using this jQuery solution:
[C#][Selenium] How to drag-hover-drop an element

Related

WebElement click() not working in Selenium

I was learning automated selenium testing in https://www.yatra.com/etw-desktop/. While trying to click an image button named 'Asia'(image is attached) ,I am getting a Time out exception. Please help me in figuring out what's going wrong .
driver.manage().window().maximize();
driver.get("https://www.yatra.com/etw-desktop/");
driver.manage().timeouts().implicitlyWait(4000, TimeUnit.MILLISECONDS);
Thread.sleep(5000);
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable
(By.xpath("//*[#id=\"scrollable1\"]/div[1]/div/a[2]/div[4]"))).click();
Thread.sleep(4000);
Assert.assertEquals("https://www.yatra.com/etw-desktop/city-list",driver.getCurrentUrl());
image showing the web element to be clicked
Try this:
How to Find Web Elements in Shadow DOMs using Selenium
It is most probably because of using Thread.sleep(), which is the worst case of explicit wait. Instead use the wait method provided by selenium itself.

Selenium WebDriver -- Unable to click on hyperlink, click goes to the other element

I am unable to click on Website hyperlink, the click goes to Recently used pages.
Tried with CSS locator of Website icon [which works in the lower environment as it doesn't have Recently used pages] reference in it.
Tried with XPath Locator[including custom XPath], still, the click goes to another item.
Tried name locator.
Used Actions class for clicking.
Allowed the page to load completely by using sleep and WebDriver wait.
Located the element and send Enter keys, still Recently used pages is clicked.
Tried to click it using coordinates.
Thought of ChromeDriver issue but the issue persists in Firefox too.
Tried below XPath:
html/body/div/div[2]/div[2]/div[1]/a/div
//div[2]/div/a/div
Code snippet:
WebElement elementToClick = driver.findElement(By.cssSelector(".icon.siteadmin"));
elementToClick.click();
WebElement elementToClick = driver.findElement(By.cssSelector(".icon.siteadmin"));
(JavascriptExecutor)driver).executeScript("window.scrollTo(0,"+elementToClick.getLocation().x+")");
elementToClick.click();
WebElement elementToClick = driver.findElement(By.cssSelector(".icon.siteadmin"));
Actions actions = new Actions(driver);
actions.moveToElement(elementToClick);
actions.click().perform();
Actions builder = new Actions(driver);
builder.moveToElement(elementToClick, 40, 207).click().build().perform();
Result: It clicks on Recently Used Pages, and it yields a result of Recently used pages instead of Website.
UI Reference
Development Code Snippet
Hope It Helps you:
.//div[#id='box_2']/a/div[#class='icon siteadmin']/div[1]
Try the following:
driver.findElement(By.XPath(“//a[contains(#title, ‘Websites’)]”)).click()
If this doesn’t work use the above XPath in combination with one of the moves to element paths above and then use click.

NoSuchWindowException - Selenium 3.4.0 - IE 11

I have launched IE 11 browser,
I have navigated to a initial URL --> done mouse over and clicked a link --> it redirects to another page.
In that page, I have to click a button, but it is throwing an exception
org.openqa.selenium.NoSuchWindowException: Unable to find element on closed window
but the windows still available on screen.
This is my code
WebElement e = driver.findElement(By.xpath("html/body/div[2]/div/div/header/nav/ul/li[2]/a"));
Actions a = new Actions(driver);
a.moveToElement(e).build().perform();
driver.findElement(By.xpath("//*[#id='menu-item-35']/a")).click();
TimeUnit.SECONDS.sleep(5);
// Exception is occurs after this, but when I delete the below code, the test case passes
driver.findElement(By.xpath("//*[#id='default_products_page_container']/div[3]/div[2]/form/div[2]/div[1]/span/input")).click();
This is the URL of the page: http://store.demoqa.com/
It looks to me like this is a race-condition error. I had those cases myself where I could actually see a window, but Selenium still said it wasn't there.
Have you tried setting the sleep time to a higher value?
You could also try to put a expect-wait-condition before clicking your element.
WebDriverWait wait = new WebDriverWait(DRIVER, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id='default_products_page_container']/div[3]/div[2]/form/div[2]/div[1]/span/input"))
Here is the Solution to your Question:
A few words about the solution:
The xpath html/body/div[2]/div/div/header/nav/ul/li[2]/a looks petty vulnerable to me use linkText locator.
Once you use Action Class to build().perform(), induce a bit of wait before locating another element.
Instead of xpath locator of //*[#id='menu-item-35']/a element, use linkText locator.
Again the xpath //*[#id='default_products_page_container']/div[3]/div[2]/form/div[2]/div[1]/span/input looks petty vulnerable to me use a logical uniquexpath.
Here is your own working code block with some simple tweaks in it:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement e = driver.findElement(By.linkText("Product Category"));
Actions a = new Actions(driver);
a.moveToElement(e).build().perform();
driver.findElement(By.linkText("iMacs")).click();
driver.findElement(By.xpath("//input[#name='Buy']")).click();
Let me know if this Answers your Question.

Webdriver do not load scroll

I have a problem that is occuring some times.
The page is not showing the scroll, so when I need to find/click an element that is not shown in the screen, I get an exception:
(org.openqa.selenium.ElementNotVisibleException: element not visible
Refreshing the page works, but I would like to know if there is other solutions for this issue.
You can simulate scroll using moveToElement from Actions class
// Java syntax, but similar to other languages
Actions actions = new Actions(driver);
WebElement elementToMoveTo = driver.findElement(...);
actions.moveToElement(elementToMoveTo).perform();

Selenium Webdriver: how to drag element to another iframe?

I am using Selenium chrome webdriver with java, I need help dragging an element out of an iframe and onto another element in a different iframe. Has anyone done this before and if so could you post an example for me?
Here is what I've tried so far, also I have tried many other solutions:
WebElement Atom = driver.findElement(By.xpath("//*[text()='Circle']"));
driver.switchTo().defaultContent();
driver.switchTo().frame("treeNavigation");
WebElement Event = driver.findElement(By.xpath("//*[text()='Line']"));
// Create Actions object passing in a WebDriver object
Actions builder = new Actions(driver);
// Chain some calls together and call build
Action dragAndDrop = builder.clickAndHold(Atom)
.moveToElement(Event)
.release(Event)
.build();
// Perform the actions
dragAndDrop.perform();
Haven't done that yet, but I would like to try the following;
move to the source iframe, get the element, clickAndHold, build, perform. Then switch to default content, then switch to target iframe, moveToElement, release, build, perform.
Any sample test page would help. Thanks.