Is it possible to perform drag&drop action to some offset position in Selenide? I want to drag element to some part of the page. Unfortunately I cannot find the answer on other pages and documentation lacks it too. I wanted to avoid using selenium tools like "Action" class. It necessarily has to be moved to offsetX and offsetY.
Actions dragAndDrop = new Actions(driver);
dragAndDrop.dragAndDropBy(element, offsetX, offsetY).perform(); -> I want to replace it with some Selenide tool
try to use Selenide.actions() static method.
From Selenide javadoc:
public static org.openqa.selenium.interactions.Actions actions()
With this method you can use Selenium Actions like described in the AdvancedUserInteractions page.
actions()
.sendKeys($(By.name("rememberMe")), "John")
.click($(#rememberMe"))
.click($(byText("Login")))
.build()
.perform();
Related
I have tried all the possible things. also, tried searching over tried different combinations and variations. I can get the element clicked which opens the dropdown. but i am not able to select an option in it. I tried with actions, sendkeys, keys.down/enter and multiple things as well. however, it didn't help. That's the only thing I am stuck on.
//selecting office
1.driver.findElement(By.id("DG5QEPn")).click();
Actions actions = new Actions(driver); actions.moveToElement(driver.findElement(By.xpath("//*[#id="DG5QEPn"]/div/div/div1/div1"))).click();
driver.findElement(By.id("DG5QEPn")).click();
driver.findElement(By.id("DG5QEPn")).sendKeys("RTP HQ"); driver.findElement(By.id("DG5QEPn")).sendKeys(Keys.Down); driver.findElement(By.id("DG5QEPn")).sendKeys(Keys.Enter);
Html-
In the below image.
You should enumerate through all of the options then click on the one you want. Take a look at this.
EDIT
The easiest way that I have found was to do something along the lines of:
el = driver.find_element_by_id('id_of_select')
for option in el.find_elements_by_tag_name('option'):
if option.text == 'The Options I Am Looking For':
option.click() # select() in earlier versions of webdriver
break
Below code worked for me
WebElement selectMyElement = driver.findElement(this.getObject(By.Id("Id of Your DropDown")));
selectMyElement.click();
Actions keyDown = new Actions(driver);
keyDown.sendKeys(Keys.chord(Keys.DOWN, Keys.DOWN, Keys.ENTER)).perform();
In several threads here, there is a work-around posted for selenium drag and drop with pages that use HTML5 for drag and drop. This work-around involves using javascript to simulate the drag and drop, for example Unable to perform HTML5 drag and drop using javascript for Selenium WebDriver test, and https://gist.github.com/rcorreia/2362544. This solution works well on this page, http://the-internet.herokuapp.com/drag_and_drop.
The general approach is to read the javascript file here (https://gist.github.com/rcorreia/2362544#file-drag_and_drop_helper-js) into a string, referred to as 'jsfile' below.
then in selenium (with java), pass in the css selectors for the source and the destination, where #column-a is the id of the source and #column-b is the target.
((JavascriptExecutor) driver).executeScript(jsfile +"$('#column-a').simulateDragDrop({ dropTarget: '#column-b'});");
It works like a champ on that page.
However, a similar approach does not seem to work on this page, https://crossbrowsertesting.github.io/drag-and-drop.html. Nothing happens when I run
((JavascriptExecutor) driver).executeScript(jsfile +"$('#draggable').simulateDragDrop({ dropTarget: '#droppable'});");
I have pages that seem to behave like this second page (eg no drag and drop). As a first step in understanding this, I'd like to get an idea why this approach does not seem to work in the latter case here.
On re-testing https://crossbrowsertesting.github.io/drag-and-drop.html, it looks like the straight-forward use of the Actions class does the trick for drag and drop. In the particular app that I am testing, which is set up with some additional code to help with accessibility, I was able to get drag and drop happening by setting focus on the first element and hitting the return key, then setting the focus on the target element and hitting return again. I am fairly sure that this is custom event handling, so may not work in other applications. Just in case, I've posted code here which does this in selenium.
public void dndHtml5(String xPathSource, String xPathDestination) {
clickEnterKeyOnElement(xPathSource);
clickEnterKeyOnElement(xPathDestination);
}
public void clickEnterKeyOnElement(String xPath) {
setFocusOnElement(xPath);
WebElement target=element(xPath);
target.sendKeys(Keys.ENTER);
}
public void setFocusOnElement(String xPath) {
WebElement element = element(xPath);
Actions actions = new Actions(driver);
actions.moveToElement(element).build().perform();
}
public WebElement element(String xPath){
return driver.findElementByXPath(xPath);
}
I am using Selenium Webdriver(Java) for my Automation. For one of my use-case, I need to click based on co-ordinates. I am using following code to perform this operation:
Actions act = new Actions(driver);
act.moveByOffset(236, 92).click().perform();
Above code is working perfectly in Firefox(Gecko driver). But with Chrome driver, it is not working. Any idea?
Is there any other way to perform this.
I think you can try what Santosh has suggested
act.moveByOffset(236, 92).click().build().perform();
However, this should not make much of a difference as the perform() already contains the build action but this might be a workaround for your problem.
If you can locate the webelement, you can use JavaScript to perform the click this way:
JavaScriptExecutor js = (driver)JavaScriptExecutor;
js.executeScript("arguments[0].click();", element);
I'm using Selenium 3.0.1 for running automation tests using TestNG.
In one test I'm trying to hover on an action menu and then click an option in that menu:
Actions builder = new Actions(getWebDriver());
builder.moveToElement(actionButton).build().perform();
But the test is not stable. I can see the menu opens but immediately closing, so the test fails because it's not finding the option any more.
I'm receiving this error:
java.lang.IllegalArgumentException: Must provide a location for a move action.
at org.openqa.selenium.interactions.MoveMouseAction.<init>(MoveMouseAction.java:30)
at org.openqa.selenium.interactions.Actions.moveToElement(Actions.java:251)
How can I check if the menu is open? the perform() method is returning void.
I notice if I put call the moveToElement twice, than the test is being more stable. Is there any elegant option of doing so?
Actions builder = new Actions(getWebDriver());
builder.moveToElement(actionButton).build().perform();
builder.moveToElement(actionButton).build().perform();
This how the menu looks like when we hover over it:
I find this issue:
https://sqa.stackexchange.com/questions/3467/issue-with-losing-focus-of-hover-command-when-the-mouse-is-outside-of-the-acti
which explains best my problem. unfortunately, still with no solution.
If it is not necessary for you to open the menu, please try clicking the option using JavascriptExecutor. JavascriptExecutor can click a hidden element as well, all that is necessary for the click to be triggered using JavascriptExecutor is that the element is present on the DOM.
Snippet (Java):
((JavascriptExecutor)driver).executeScript("arguments[0].click()", driver.findElement(By.cssSelector("hiddenOptionFromMenu")));
You can wait for the menu to appear after the hover with a FluentWait, like so:
FluentWait<> wait = new FluentWait<>(getWebDriver())
.withTimeout(driverTimeoutSeconds, TimeUnit.SECONDS)
.pollingEvery(500, TimeUnit.MILLISECONDS)
.ignoring(StaleElementReferenceException.class)
.ignoring(NoSuchElementException.class)
.ignoring(ElementNotVisibleException.class)
wait.until(x -> { return driver.findElement(menuElementBy); } );
If the mouse hover succeeded - the menu starts appearing - there's no reason you need to call it twice.
It seems like a timing issue.
If the menu has a transition effect, then add a delay of the duration of the effect:
new Actions(driver)
.moveToElement(menu)
.pause(100) // duration of the transition effect in ms
.perform();
submenu.click();
You could also wait for the targeted element to become visible and steady (same position returned twice in a row).
Is there any examples of Javascript or jQuery been used with Selenium Webdriver Java? I'm having issues with drag and drop using selenium. I'm hoping to use the following code with Javascript/jQuery to perform a drag and drop. I cannot use point and click in my test scripts to perform drag and drop so I'm hoping to use jQuery or java script to perform the drag and drop. I'm having trouble integrating the two. I only want suggestion for drag and drop using javascript or an example of javascript (any) and selenium webdriver code in the same script
public void test(){
driver.findElement(By.id("addService")).click();
driver.findElement(By.id("name")).sendKeys(name);
driver.findElement(By.id("identifier")).sendKeys(id);
driver.findElement(By.id("flowStatus")).clear();
driver.findElement(By.id("flowStatus")).sendKeys(flow);
}
public void dragAndDropElement(WebElement dragFrom, WebElement dragTo) throws Exception {
Actions actions = new Actions(driver);
actions.clickAndHold(dragFrom).release(dragTo).build().perform();;
}
public void test() throws Exception {
WebElement dragFrom = driver.findElement(By.xpath("/html/body/div/div[2]/div[1]/form/fieldset/table[1]/tbody/tr/td[1]/div/div[1]"));
WebElement dragTo = driver.findElement(By.id("drop"));
dragAndDropElement(dragFrom,dragTo);
}
Have tried using Actions class? Here are docs. There are multiple ways you could drag and drop elements using it,
Actions actions = new Actions(driver);
actions.dragAndDrop(source,target).build().perform();
or
actions.clickAndHold(source).release(target).build().perform();
There are other ways as well, check docs and see which one is applicable for you. In general using javascript should be avoided when using WebDriver. WebDriver uses browsers native api's and simulate user interactions which are very close to a real user. Think of this, your user is not going to execute a javascript to do a drag and drop. I would suggest use it only when all other WebDriver doors are closed for you.