Selenium rookie here with a question regarding drag and drop.
I have two boxes and from one of the boxes i can drag a line and connect it to the other box.
I have tried various ways but none of them seem to work.
Actions action = new Action(driver);
action.dragAndDrop(box1 , box2).perform(); // Did not work
action.clickAndHold(box1).moveToElement(box2).release().perform(); //did not work either
action.clickAndHold(box).moveByOffset(coordonates of box2).release().perform(); // also did not work
This is were my knowledge of selenium stops , is there any other way i can do this?
I know the xpath is good because when i perform element.click(); it access it so its not that.
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
I am using Selenium to run tests on a page with multiple drop-down menus (specifically a pop-up page which allows you to select some options then close it). I am able to click on some of these menus totally fine; however, some of them throw an ElementNotInteractable exception with the message "element could not be scrolled into view", even though the menus are right beside each other. I am very confused as to why one menu works and the other does not even though they appear to be the same. The three things I have tried in order to click on the menu are:
a) Regular Selenium clicking :
driver.findElement(By.xpath("//select[#foo='bar']").click();
This is what works with the other menus, except I navigate directly to the "option" tag and click on it (don't need to click the drop down first)
b)Javascript executor
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
When I use this, no exceptions are thrown; however, the menu remains empty, which leads me to believe it is not being clicked on.
c)Actions
Actions builder = new Actions(driver);
builder.moveToElement(menu).click(menu);
builder.perform();
For some reason when I use this, the whole pop-up window with the drop down menus on it closes. :/ (I have double checked that it is not the close button being clicked)
I'm not sure if this is relevant, but Selenium has no problem finding the elements, it is just when I try to click them that it complains.
To summarise, my questions are:
1) What could make the menus different such that one is clickable and one is not?
2) How can I click on the second menu and choose an option?
Edit: I tried the solution found in the similar problem; unfortunately it does not work. The solution was to add an explicit wait since the element may not have completely loaded, this only leads to a timeout.
Using JavascriptExecutor is a workaround to interact with non interactable elements. I think it should never be used in selenium tests because it makes the test do things a real user wouldn't be able to do in a real life scenario.
The most plausible cause is that you are interacting with the wrong element, try to debug to identify the element returned by the used selector.
You can use chrome dev tools in debug mode :
1- Put a breakpoint at the exception line,
2- Use $x("//select[#foo='bar']") in the chrome console to get the element.
To select a value, you can use the org.openqa.selenium.support.ui.Select object :
new Select(element).selectByValue(value);
Using Selenium w/ Java bindings and ChromeDriver 2.3 with the latest browser installed
I'm currently stumped after more than enough time trying to find a solution. I'm currently trying to drag and drop an element to another element. The only catch is the element target I need to move to is only visible once I move the source element. Any advice?
So here a screenshot of the source element that I click and hold, and then dragging it exposes the two options that I can drop too:
Element that is the source
So as I hold down the mouse and drag just a small portion, the drop targets become visible, per the screenshot below:
Targets visible once mouse is dragged with source
Here is the latest code snippet I've tried that I believe should make this happen, but yet nothing happens and continues into the Thread.sleep(), which was only put in for observation purposes. These are assuming healthy instances of WebDriverWait and WebDriver:
driverWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='user-info ng-binding'][text()='Sample Text']")));
driverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='user-info ng-binding'][text()='Sample Text']")));
source = driver.findElement(By.xpath("//div[#class='user-info ng-binding'][text()='Sample Text']"));
//move to element, click and hold, and then move it to expose the available options
actions.moveToElement(source).clickAndHold().pause(Duration.ofSeconds(1)).moveByOffset(10, 10).pause(Duration.ofSeconds(1)).build().perform();
Thread.sleep(10000);
So the idea behind this is to just click and hold the source, move it a little to display the targets, and then find the targets, and use the actions.release() to move the target onto the source, but when I debug it it does nothing on the element to the element and goes straight to the Thread.sleep(). I've read about plenty of bugs. Any advice would be greatly appreciated.
I know this is an old question but I was looking for a solution but in the end I just made the hidden area visible. I know this isn't ideal but seems to work.
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('style', 'display:block')", targetElement);
We have diagram application which is built using Canvas and GoJS. There are many controls in toolbox and I would like to drag particular element to blank area.
I have tried by actions method but that is not working.
I tried code :
actions.moveToElement(paletteCanvas, palette_node_x, palette_node_y);
actions.clickAndHold();
//Dragging selected node a little bit to make it work.
actions.moveToElement(paletteCanvas, palette_node_x, palette_node_y+50 );
//Now perform the actual move
actions.moveToElement(flowCanvas, diagramOffsetX , diagramOffsetY);
actions.release();
actions.perform();
Also it is hard to find offsets for element. Is there any there way by which I can automate following using selenium :
Drag and Drop any control fro toolbox
Select any particular control from tool box
Any possibility to work by JSexecutor?
These links might be helpful to you:
http://forum.nwoods.com/t/how-to-implement-selenium-test-cases-for-canvas/5195
http://forum.nwoods.com/t/locating-elements-on-canvas-by-selenium-webdriver/5509
http://forum.nwoods.com/t/simulate-selectionadorrnmenttemplate-click-selenium/6134
I have looked around and could not find any threads with a solution to this yet. I am trying to make a program for a specific website, it interacts with the website just like user would do and it does so how the user wants it to interact. I'm using Selenium right now and just grabing WebDriver and making it do a list of tasks, but I want the WebDriver to interact how the user would want it to.
So is there any way to lay a "skin" on the WebDriver? So like a button where if selected it does a certain task? And is there a way to remove the URL bar and make a gradient border around the Browser to make it look like a program? Or should I be using something else in order to accomplish this?
Here is an "image" of what it would look like:
http://i39.tinypic.com/okn0rd.jpg
Where only the black is visible and the brown isn't there, or not visible and there are "buttons" over?
So would this be possible with any WebDriver through Selenium or should I be using other ways to interact with the webpage in order to accomplish this? Don't need to do my homework for me, but I would appreciate refrences to where I could do this. I was thinking of using HTMLUnit and just making a headless browser that does all this in the background and prints a picture and I can display that in a Java Program with all the buttons over it?
I'm using selenium of Java for automation tests and the browser is Firefox.
This is my sample code:
WebElement elem = driver.findElement(By.xpath(".//*[#id='main']/div/div[3]/div[1]/div/div[3]/div/div/div/a"));
Actions action = new Actions(driver);
Actions action2 = action.moveToElement(elem);
action2.perform();
The problem is that "moveToElement" action is triggering auto scrolling event of the page.
I want the page to remain as it was before without the scrolling.
somebody may know how can I disable this auto scrolling?
Thanks.
You can't. WebDriver scrolls elements into view when acting on them.
You can't disable the autoscroll.
Some possible workarounds:
You could try to fire a syntetic mouseover event over your WebElement if that would help your cause.
After the moveToElement(), you could try rescroll to your needed position with window.scrollTo() or for example the Page Up key. This would, obviously, break the mouseover on the element, but maybe it's what you need.
You can position your real mouse cursor over the element via the Robot class. This might get a little tricky, you might need to enter the fullscreen mode with your browser (or use this) and then handle the scroll offsets manually, if there are some.
All depends on your intentions, on what you really need to do with the element and why.