Drag and drop in Selenium - selenium

In https://www.globalsqa.com/demo-site/draganddrop/ I need to drag and drop picture with the text "High Tatras" into Trash section.
#Test
void task1() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\DELL\\Desktop\\New folder\\chromedriver.exe");
WebDriver webDriver = new ChromeDriver();
try {
webDriver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
webDriver.get("https://www.globalsqa.com/demo-site/draganddrop/");
WebElement from = webDriver.findElement(By.xpath("//*[#id=\"gallery\"]/li[1]"));
WebElement to = webDriver.findElement(By.xpath("//*[#id=\"trash\"]"));
Actions actions = new Actions(webDriver);
actions.dragAndDrop(from, to).build().perform();
} finally {
webDriver.close();
}
}

Both the from and to elements are inside the iframe.
So, to access these elements you need to switch into that iframe first.
Your code will look as following:
#Test
void task1() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\DELL\\Desktop\\New folder\\chromedriver.exe");
WebDriver webDriver = new ChromeDriver();
try {
webDriver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
webDriver.get("https://www.globalsqa.com/demo-site/draganddrop/");
driver.switchTo().frame(driver.findElement(By.xpath("//div[#rel-title='Photo Manager']//iframe")));
WebElement from = webDriver.findElement(By.xpath("//*[#id='gallery']/li[1]"));
WebElement to = webDriver.findElement(By.xpath("//*[#id='trash']"));
Actions actions = new Actions(webDriver);
actions.dragAndDrop(from, to).build().perform();
} finally {
webDriver.close();
}
}

Related

selenium webdriver : input data NOT inserted

Below is a text input box Search for
in selenium, i can first find the element, input data. but on the front end i dont see any data inputted
I am not sure why my UI doesnt show data being typed by the webdriver . no error in the code log
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\rahul\\Downloads\\chromedriver_win32_83\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("http://testingapp.workspez.com");
driver.manage().window().maximize();
WebElement username = driver.findElement(By.id("field_email"));
WebElement password = driver.findElement(By.id("field_password"));
WebElement login = driver.findElement(By.xpath("//*[text()='Log In']"));
username.sendKeys(Keys.CONTROL + "a");
username.sendKeys(Keys.DELETE);
password.sendKeys(Keys.CONTROL + "a");
password.sendKeys(Keys.DELETE);
username.sendKeys("rahul#workspez.com");
password.sendKeys("Sujeet#19");
login.click();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
WebElement generalInfo = driver.findElement(By.xpath("//*[text()='General Info']"));
generalInfo.click();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
WebElement Contacts = driver.findElement(By.xpath("//*[text()='Contacts']"));
Contacts.click();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//find search for
WebElement newlyaddedcontact1 = driver.findElement(By.xpath("//*[text()='Search for']"));
JavascriptExecutor executor1 = (JavascriptExecutor) driver;
executor1.executeScript("arguments[0].click();", newlyaddedcontact1);
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].value='searchcontact';", newlyaddedcontact1);
WebElement search = driver.findElement(By.xpath(".//*[#class='MuiButtonBase-root MuiIconButton-root']"));
search.click();
how to access that webpage
1.login into http://testingapp.workspez.com
username :rahul#workspez.com password: Sujeet#19
click general info tab on top, then click contacts 'tab'
you will see the search for box.
I thing You need to change you Xpath.Because whatever you write XPath this is for a label you need to write for input control so i update your code.
IWebElement newlyaddedcontact1 = driver.FindElements(By.XPath("//input[contains(#class,'MuiInput-input')]")).ToList()[0];
JavascriptExecutor executor1 = (JavascriptExecutor) driver;
executor1.executeScript("arguments[0].click();", newlyaddedcontact1);
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].value='"+name+"';", newlyaddedcontact1);
Please Try this code. I write in C# language.

appium issue, not able to launch two apps

I am not able to launch two apps in single script using APPIUM, it shows error as
An unknown server-side error occurred while processing the command.
The desired should not include both of an 'appPackage' and a
'browserName'
public class SalesLeadProg {
public AndroidDriver<MobileElement> driver;
private WebElement element; public WebDriverWait wait;
#BeforeMethod
public void setup() throws MalformedURLException, InterruptedException {
DOMConfigurator.configure("log4j.xml");
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("automationName", "UiAutomator2");
caps.setCapability("deviceName", "sunil");
caps.setCapability("udid", "422b21e7");
caps.setCapability("platformName", "Android");
caps.setCapability("platformVersion", "8.1.0");
caps.setCapability("appPackage", "com.mfcwl.mfc_dealer");
caps.setCapability("appActivity", "com.mfcwl.mfc_dealer.Activity.SplashActivity");
caps.setCapability("autoAcceptAlerts", true);
caps.setCapability("noReset", true);
caps.setCapability("browserName", "Chrome");
//it will launch the chrome browser in mobile
caps.setCapability(ChromeOptions.CAPABILITY, true);
ChromeOptions options=new ChromeOptions();
options.setExperimentalOption("androidPackage", "com.android.chrome");
caps.setCapability("autoGrantPermissions", true);
// to allow the permission of contacts camara, which comes in the begining when we install the app*/
caps.setCapability("unicodeKeyboard", true);
// to make the keyboard working
caps.setCapability("resetKeyboard", false);
// it
try {
driver = new AndroidDriver<MobileElement> (new URL("0.0.0.0:4723/wd/hub"),caps);
}
catch(MalformedURLException e) {
System.out.println(e);
}
driver.manage().timeouts().implicitlyWait(16, TimeUnit.SECONDS);
}
#Test
public void Dashboard() throws MalformedURLException, InterruptedException {
MobileElement email = driver.findElement(By.id("com.mfcwl.mfc_dealer:id/email"));
Thread.sleep(4000);
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(email));
boolean isElementPresent = email.isDisplayed();
System.out.println(isElementPresent);
Thread.sleep(4000);
driver.findElementById("com.mfcwl.mfc_dealer:id/email").clear();
driver.findElementById("com.mfcwl.mfc_dealer:id/email").sendKeys("prestige");
System.out.println("Entered ID");
driver.findElement(By.id("com.mfcwl.mfc_dealer:id/password")).clear();
driver.findElement(By.id("com.mfcwl.mfc_dealer:id/password")).sendKeys("Mahindra");
System.out.println("Entered Password");
driver.findElement(By.id("com.mfcwl.mfc_dealer:id/email_sign_in_button")).click();
//to login
System.out.println("Signed IN");
Thread.sleep(4000);
driver.findElement(By.id("com.mfcwl.mfc_dealer:id/homeTab")).click();
//to click on home button
System.out.println("Clicked on Home button");
Thread.sleep(3000); //Set ChromeDriver location
System.setProperty("webdriver.chrome.driver", "./ChromeDriver/chromedriver.exe");
driver.get("google.com/"); driver.findElement(By.id("com.mfcwl.mfc_dealer:id/click_lead")).click();
System.out.println("Clicked on Today's follow up leads section");
driver.findElement(By.id("com.mfcwl.mfc_dealer:id/lead_filter")).click();
Please assist with a solution on how to use two apps in APPIUM with a single script.

selenium webdriver slider error

This is not working. I want the slider to move. All the buttons of the slider have the same xpath. I have need to move only the departure slider option, how to do it. I have tried the below code in two ways but nothing works.
public class task {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "g://geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.cheapoair.com/");
driver.manage().deleteAllCookies();
driver.findElement(By.xpath(".//*[#id='ember746']")).sendKeys("DFW");
driver.findElement(By.xpath(".//*[#id='ember751']")).sendKeys("JFK");
driver.findElement(By.xpath(".//*[#id='owFlight']")).click();
driver.findElement(By.xpath(".//*[#id='departCalendar_0']")).click();
driver.findElement(By.xpath(".//*[#id='calendarCompId']/section/div/div[1]/ol/div[26]/li")).click();
driver.findElement(By.xpath(".//*[#id='ember751']")).sendKeys("JFK");
driver.findElement(By.xpath(".//*[#id='owFlight']")).click();
driver.findElement(By.xpath(".//*[#id='ember730']/section/form/input")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Actions a = new Actions(driver);
//WebElement target = driver.findElement(By.xpath(".//*[#id='DivDepart']/div/div/div[1]/div[2]"));
//WebElement source = driver.findElement(By.xpath(".//*[#id='DivDepart']/div/div/div[1]/div[1]"));
//a.dragAndDrop(source, target).build().perform();
WebElement slider = driver.findElement(By.cssSelector("div[class='slider-handle round']"));
a.clickAndHold(slider).moveByOffset(30, 0).release(slider).build().perform();
}
}

How to select elements from autosuggest box using selenium webdriver

I m sending the firepath screenshot
I want to select first element and display in element text box
Can someone please help in this
You can update your code as below:
public class SelectAutoSugggestedValue {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("http://www.google.com");
driver.findElement(By.id("gbqfq")).sendKeys("automation tutorial");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
List allOptions = driver.findElements(By.xpath("//td/span[text()='automationtutorial']"));
for (int i = 0; i < allOptions.size(); i++) {
String option = allOptions.get(i).getText();
System.out.println(option);
}
}
}

To perform Autosuggestion using Selenium

Having some problem in selecting the option from the dropdown using autosuggestion. Please give a solution to select the option.
The related code is posted below :-
#Test(priority = 4)
public void ReportType() throws InterruptedException {
WebElement reporttype = driver.findElement(By.xpath("html/body/form/div[3]/table[2]/tbody/tr[3]/td/table/tbody/tr[1]/td/table/tbody/tr[2]/td/div/span/table/tbody/tr[3]/td[2]/span/table/tbody/tr/td[2]/input[1]"));
reporttype.clear();
reporttype.sendKeys("NMQ De");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("html/body/form/div[3]/table[2]/tbody/tr[3]/td/table/tbody/tr[1]/td/table/tbody/tr[2]/td/div/span/table/tbody/tr[3]/td[2]/span/div/ul/li[4]")));
Thread.sleep(5000);
driver.findElement(By.xpath("html/body/form/div[3]/table[2]/tbody/tr[3]/td/table/tbody/tr[1]/td/table/tbody/tr[2]/td/div/span/table/tbody/tr[3]/td[2]/span/div/ul/li[4]")).click();
}
Think your question as google search when u do some google search google provides u auto suggestion
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
String textToSelect = "headlines today";
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
Thread.sleep(2000);
WebElement autoOptions= driver.findElement(By.id("lst-ib"));
autoOptions.sendKeys("he");
List<WebElement> optionsToSelect = driver.findElements(By.xpath("//div[#class='sbqs_c']"));
for(WebElement option : optionsToSelect){
System.out.println(option);
if(option.getText().equals(textToSelect)) {
System.out.println("Trying to select: "+textToSelect);
option.click();
break;
}
}