Why can't i drag and drop in selenium? - java

This is website: https://html5demos.com/drag/
And this is my code perform drag and drop element using java, chrome and selenium 3.141.59
By dustBin = By.xpath("//div[#id=\"bin\"]");
By one = By.xpath("//a[#id=\"one\"]");
WebElement From = driver.findElement(one);
WebElement To = driver.findElement(dustBin);
Actions actions = new Actions(driver);
actions.dragAndDrop(From,To).build().perform();
actions.clickAndHold(From).moveToElement(To).build().perform();
actions.dragAndDropBy(From,176,260).build().perform();
When I run test with TestNG the element is not moving to dustBin. So I dont know why. Any one help me resolve this issue. Thank you.

You should use findElementById to find locator instead of xpatch.
Try this one:
WebElement sourceLocator = driver.findElement(By.id("one"));
//To get target locator
WebElement targetLocator = driver.findElement(By.id("bin"));

Related

While trying to automate facebook signup page through selenium webdriver the page is getting loaded in hindi as default? How to make it open in eng?

The same being opened in an brower without automating is opening in english.Using the below Code:
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("https://www.facebook.com/r.php?locale=EN_US&campaign_id=973072070&extra_1=s%7Cc%7C256741383347%7Ce%7Cfacebook%20sign%20up%7C&placement=&creative=256741383347&keyword=facebook%20sign%20up&partner_id=googlesem&extra_2=campaignid%3D973072070%26adgroupid%3D54006292691%26matchtype%3De%26network%3Dg%26source%3Dnotmobile%26search_or_content%3Ds%26device%3Dc%26devicemodel%3D%26adposition%3D%26target%3D%26targetid%3Dkwd-295862466660%26loc_physical_ms%3D20464%26loc_interest_ms%3D%26feeditemid%3D19894516786%26param1%3D%26param2%3D&gclid=EAIaIQobChMIhae5qNH98AIVtdxMAh18Rw2UEAAYASABEgJvEvD_BwE");
Thread.sleep(5000);
WebElement day = driver.findElement(By.id("day"));
WebElement month = driver.findElement(By.id("month"));
WebElement year = driver.findElement(By.id("year"));
Select select = new Select(day);
select.selectByVisibleText("5");
Select select1 = new Select(month);
select.selectByVisibleText("April");
Select select2 = new Select(year);
select.selectByVisibleText("1995");
I don't know why the site is opening in Hindi, that doesn't happen for me. In any case, I guess you can just tell selenium to switch language by identifying and clicking the right language link. I assume you want English?
// code to open the page
driver.findElement(By.xpath("//*[text() = 'English (US)']")).click()
//rest of your test
Just change your facebook link instead of "http://facebook.com" use "http://en-gb.facebook.com
You can set the ChromeDriver locale with the following code:
ChromeOptions options = new ChromeOptions();
options.addArguments("--lang=en-US");
ChromeDriver driver = new ChromeDriver(options);

Selenium Webdriver - Drag and drop does not work

I know there are several posts about this issue. Tried almost all the solutions in my project but unable to make drag and drop work. I am using following code:
WebDriver driver = new ChromeDriver();
WebElement dragableElement =
driver.findElement(By.className("dragElement"));
WebElement dropableContainer = driver.findElement(By.xpath("//*
[#id='contentCollection_xyz']"));
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(dragableElement)
.moveToElement(dropableContainer)
.release(dropableContainer)
.build();
dragAndDrop.perform();
This drops my dragable element just before dropable container.
I have gotten drag and drop to work with selenium, this is a poorly worded question though. We need more details, and stack overflow is not the place to find someone to "share your screen with".

Not able to save page in selenium webdriver

At the end, I am not able to click on save button. The last click is not working for me.
System.setProperty("webdriver.chrome.driver" , "C:\\Users\\gausia.fatima\\Downloads\\automation\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.idfystaging.com/users/sign_in");
driver.findElement(By.name("user[login]")).sendKeys("themanager#mailinator.com");
driver.findElement(By.name("user[password]")).sendKeys("Password123");
driver.findElement(By.name("commit")).click();
driver.get("http://www.idfystaging.com/profiles/new?company_id=189");
Select mydrpdwn = new Select(driver.findElement(By.id("company_id")));
mydrpdwn.selectByVisibleText("Fringe A Division");
driver.findElement(By.id("email_addresses")).sendKeys("awesome1#mailinator.com");
driver.findElement(By.id("counts_aadhaar")).sendKeys("1");
//WebDriverWait wait = new WebDriverWait(driver,30);
//wait.until(ExpectedConditions.presenceOfElementLocated(By.name("commit")));
driver.findElement(By.name("commit")).submit();
System.out.println("candiate is invited.");
//driver.close();
In the "Invite Candidates" page, you have to submit button with the same name as "commit", so selenium is throwing error.
http://www.idfystaging.com/profiles/new?company_id=189
Use the CssSelectorto get the control and submit them.
Java code un tested but it should work.
driver.findElement(By.cssSelector("input.f21")).Submit();
Note : This is c# code and I tested
driver.FindElement(By.CssSelector("input.f21")).Submit();
as name=commit returns two elements in the page, should use more strict selector to find the Invite button.
Try the following code to click on Invite button:
driver.findElement(By.xpath("//input[#value='Invite']")).click();
or
driver.findElement(By.xpath("//input[#value='Invite']")).submit();
You can use following code. I have modified it and it is working fine. Please check if it is working fo you.
System.setProperty("webdriver.chrome.driver" , "C:\\Users\\gausia.fatima\\Downloads\\automation\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.idfystaging.com/users/sign_in");
driver.findElement(By.id("focus_on_load")).sendKeys("themanager#mailinator.com");
driver.findElement(By.id("user_password")).sendKeys("Password123");
driver.findElement(By.name("commit")).click();
Thread.sleep(5000);
driver.navigate().to("http://www.idfystaging.com/profiles/new?company_id=189");
Select mydrpdwn = new Select(driver.findElement(By.id("company_id")));
mydrpdwn.selectByVisibleText("Fringe A Division");
driver.findElement(By.id("email_addresses")).sendKeys("awesome1#mailinator.com");
driver.findElement(By.id("counts_aadhaar")).sendKeys("1");
driver.findElement(By.cssSelector("input.f21")).click();
System.out.println("candiate is invited.");

element not visible: Element is not currently visible and may not be manipulated

I'm trying to select one of the element from dropdown.I have retrieved 12 elements when I used the method getAttribute():
Select select = new Select(driver.findElement(By.xpath("//select[#id='dataset_downloadDataset_select']")));
List<WebElement> options = select.getOptions();
System.out.println(options.size());
for (int i=1; i<=11; i++){
System.out.println(options.get(i).getAttribute("value"));
After retrieving 12 elements of dropdowm,I want to select one of them.For that I have tried Actions/Javascriptexecutor but I'm getting element not visible exception.The code used for Action method is:
WebElement mnuElement;
WebElement submnuElement;
mnuElement = driver.findElement(By.xpath("//input[starts-with(#data-activates,'sele')][#value='XXXXXXXXXXX']"));
submnuElement = driver.findElement(By.xpath("//*[#id='dataset_downloadDataset_select']/option[4]"));
Actions builder = new Actions(driver);
builder.moveToElement(mnuElement).perform();
Thread.sleep(5000);
driver.findElement(By.xpath("//*[#id='dataset_downloadDataset_select']/option[4]")).click();
Could anyone help me to resolve this.
It depends on browser you are using. Chrome can click option without extending while firefox can't deal with it. Use select to choose from . Option you are using requires to click on the list and then choose, but it is simplier to do like that
Code for firefox (and probably every browser)
WebDriver driver;//then choosing browser
element=driver.findElement(By.xpath("//select[#id='dataset_downloadDataset_select']"));//or whatever
Select select=new Select(element);
//To select what you want, this is selecting, nothing more
select.selectByValue("Mainframe File 1");//or other

Selenium webdriver Java code using web driver for double click a record in a grid

How to write selenium java code for doubleClick() on a record using web driver?
I have displayed some records in the body part. Once I clicked on a record we should get a popup window to update it.
Please suggest how to write Selenium Java code using web driver.
I have tried following code:
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//table/tbody/tr[2]/td/div/div/table/tbody/tr[10]/td[1]"))).doubleClick().build().perform();
Use Actions class to perform mouse, keyboard actions on WebElements using WebDriver.
Actions action = new Actions(driver);
WebElement element=driver.findElement(By.linkText("TEST"));
//Double click
action.doubleClick(element).perform();
//Mouse over
action.moveToElement(element).perform();
//Right Click
action.contextClick(element).perform();
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//table/tbody/tr[2]/td/div/div/table/tbody/tr[10]/td[1]"))).doubleClick().perform();
This code works!!!
You should use the Actions() class as this includes a 'double-click' action.
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.linkText("Test"))).doubleClick().build().perform();
Try this code:
Actions action = new Actions(driver);
WebElement btnElement=driver.findElement("Locator of element"));
action.doubleClick(btnElement).build().perform();
You can make use of Actions class of WebDriver to perform composite actions like
Double click, Drag and Drop, Hover etc.
// Creates an instance of Actions class, passing current driver instance.
Actions builder = new Actions(driver);
Way 1:
// Gets an Action class object that holds an action/ a set of actions
Action action = builder.doubleClick(element);
// Builds the set of actions/ single action using build() and executes on browser using perform() method.
action.build().perform();
Way 2:
// Calls build() and perform() methods directly on Actions class instance
builder.doubleClick().build().perform();
And in case if have no additional actions binded to singleclick, you can use:
driver.findElement(By.xpath("%youXPath%"))).click;
driver.findElement(By.xpath("%youXPath%"))).click;
Actually, it should works in most cases (except you have some custom system doubleclick settings)
WebElement element = driver.findElement(selector);
Actions builder = new Actions(driver);
builder.doubleClick(element).perform();
I implemented Ran's (immediately above my post) solution. I'm writing Java in Eclipse and using Selenium WebDriver.
There are 2 imports that you'll need:
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
Then, I implemented the code thusly:
WebElement element = driver.findElement(By.xpath("/html/body/div[1]/div/div/div[2]/div[1]/div[3]/div[8]/div[2]/div/div[2]/div/table/tbody/tr[2]"));
Actions builder = new Actions(driver);
builder.doubleClick(element).perform();
Thanks to Ran!
I'd been struggling with this one for several hours. Invoking the single click twice doesn't work for me - too much time between the events to be captured by the browser under test as a double-click.

Categories

Resources