Drag and drop functionality not working in selenium chromedriver - selenium

I am working on a drag and drop functionality using selenium chromedriver. But unfortunately unable to achieve it. I am using following piece of code,
public void dragAndDropElement() {
try {
WebElement imgToDrag = driver.findElement(By
.className("imageclassname"));
WebElement dropHere = driver.findElement(By.cssSelector("panelcssname"));
Actions action = new Actions(driver);
Actions actions = null, movetoElement = null;
actions = action.clickAndHold(imgToDrag);
Thread.sleep(3000);
movetoElement = actions.moveToElement(dropHere);
movetoElement.perform();
movetoElement.release();
Thread.sleep(6000);
} catch (Exception e) {
logger.log(Level.SEVERE, "Exception occured in DragAndDropClass :: dragAndDropElement()", e);
}
}
After running this code, nothing is happening I mean neither it is throwing any exception nor it is performing drag and drop functionality. What mistake I am doing here, can anybody help me to get out of this problem.

Related

driver.close() will hang for forever

driver.close() is not working on Jenkins and the whole test will hang for forever. I am using Selenium Grid with Java and using Chrome Driver.
I don't want to user driver.quit(). I have to use driver.close(). I have two tabs open and i have to close one.
public static void closeBrowser()
{
try
{
WebDriver testDriver = BrowserFactory.getInstance().getDriver();
if (testDriver != null)
{
testDriver.close();
}
wait.wait(2);
Log.info("Closing the browser");
}
catch (Exception e)
{
Log.info("Cannot close browser");
}
}
This used to work and started to happen recently.
Try this following:
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "w");
This code will close the currently opened tab.
Better solution i found to close window is:
((JavascriptExecutor) BrowserFactory.getInstance().getDriver()).executeScript( "window.close()" );

Open Edge in InPrivate mode using Selenium

I am using Selenium 3.4 to launch Edge using the Microsoft WebDriver which is now maintained by Microsoft.
Is there any way I can launch the Browser in InPrivate mode using Selenium?
I have searched for answers but couldn't find any.
The closest I got was How to start Edge browser in Incognito mode using selenium remote webdriver?
The solution mentioned there doesn't work. It just shows the same tab as would be shown in InPrivate, but the window isn't a private one. As such, the information is stored and the session is not private.
Add capabilities...
Try the code below.
DesiredCapabilities capabilities = DesiredCapabilities.edge();
capabilities.setCapability("ms:inPrivate", true);
return new EdgeDriver(capabilities);
I made the capabilities work in Python like this:
from selenium.webdriver import Edge
from selenium.webdriver import DesiredCapabilities
capabilities = DesiredCapabilities.EDGE
capabilities['ms:inPrivate'] = True
driver = Edge(capabilities=capabilities)
Use the below code, employing java.awt.Robot to simulate the key combination CTRL+SHIFT+P to open a new browser window in InPrivate mode:
System.setProperty("webdriver.edge.driver","D:\\Workspace\\StackOverlow\\src\\lib\\MicrosoftWebDriver.exe"); //put actual location
WebDriver driver = new EdgeDriver();
driver.navigate().to("https://www.google.com");
driver.manage().window().maximize();
Robot robot;
try {
// This is the actual code that opens the InPrivate window
robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_P);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.keyRelease(KeyEvent.VK_P);
Thread.sleep(3000);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String parentWindowHandler = driver.getWindowHandle();
String subWindowHandler = null;
Set<String> handles = driver.getWindowHandles();
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
subWindowHandler = iterator.next();
driver.switchTo().window(subWindowHandler);
System.out.println(subWindowHandler);
}
driver.get("https://stackoverflow.com/");
//driver.switchTo().window(parentWindowHandler); // Uncomment this line if you want to use normal browser back
}
Note that we are using robot class and so if the system locks it may not work.

Unable to handle a popup

I am trying to automate a website, however I am unable to handle the popup. I tried using switch to frame and also dismiss alert but both didn't work. Below is the code for same. Could someone please help me.
Code:
#Test(priority=1)
public void Overview() throws InterruptedException {
//driver.get(baseUrl);
logger = extent.startTest("Overview");
logger.log(LogStatus.INFO, "Launching the website >>> http://www.boxer.co.za/");
driver.get("http://www.boxer.co.za/");
Thread.sleep(2000);
driver.findElement(By.xpath("//a[#id='close-icon']")).click();
logger.log(LogStatus.INFO, "Click on Promotions");
driver.findElement(By.xpath("//a[#href='http://www.boxer.co.za/on-promotion/'][contains(.,'On Promotion')]")).click();
}
Try with below code and lets see if it works-
try{
driver.switchTo().frame(0);
driver.findElement(By.xpath("//a[#id='close-icon']")).click();
driver.switchTo().defaultContent();
}
catch (Exception e){}

Selenium webdriver throwing timeout exception

I am new to Selenium.
My issue is that I'm trying to click an element but Selenium is throwing a timeout exception, even if I increase the timeout value.
Do I need to use xpath instead of id?
The HTML Code is:
My code looks like this
void searchquotation() throws TimeoutException {
try {
WebDriverWait wait = new WebDriverWait(driver, 15);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.name("SearchButton")));
element.click();
}
catch(TimeoutException e) {
System.out.println("Timeout occured");
}
Am I doing anything wrong?
The input type here is submit (by looking at your HTML code) so I would strongly advise to try the submit() function of Selenium.
Instead of By.name, you should use By.id instead. Therefore, use either of these:
By.Id("SearchButton")
By.CssSelector("input#SearchButton")
By.Xpath("//input[#id='SearchButton']")
Note: syntax could be wrong, please adjust depending on your programming language
try below code, even timeout exception occurs, it will try 4 time to click on it. assuming locator is correct By.name("SearchButton")
public void searchquotation()
int count=0;
while(count<4)
{
try {
WebElement x = driver.findElement(By.name("SearchButton")));
WebDriverWait element=new WebDriverWait(driver,15);
element.until (ExpectedConditions.presenceOfElementLocated(By.name("SearchButton")));
x.click();
count=count+4;
}
catch(TimeoutException e) {
count=count+1;
System.out.println("Timeout occured");
continue;
}
}

org.openqa.selenium.remote.SessionNotFoundException: The FirefoxDriver cannot be used after quit()

driver.findElement(By.xpath(OR.getProperty(object))).click();
System.out.println("Test");
Click works for some button.But on clicking a specific button in application.
But 'org.openqa.selenium.remote.SessionNotFoundException' erro comes after the above driver action. Test is not printed on console after that click. Why is it so?
public static void click(String object, String data){
try{
/*try
{
driver.switchTo().alert().accept();
}
catch(Exception e){}*/
new WebDriverWait(driver, 30).until(ExpectedConditions.elementToBeClickable(By.xpath(OR.getProperty(object))));
driver.findElement(By.xpath(OR.getProperty(object))).click();
System.out.println("Test");
Log.info("Clicking on Webelement "+ object);
}catch(Exception e){
Log.error("Not able to click --- " + e.getMessage());
DriverScript.bResult = false;
}
}
This is the code. Its a keyword driven framework. This action keyword gets executed 6 times perfectly. But on clicking some button which popups new window this error occurs. Switch window is supposed to be the next action keyword to be executed. But it is not reaching there . Just after .click it stands idle for long time. Then the above exception.
public static void switchwindow(String object,String data){
try{
parentHandle = driver.getWindowHandle();
System.out.println(driver.getWindowHandles().size());// get the current window handle
for (String winHandle : driver.getWindowHandles()) {
if(winHandle.equalsIgnoreCase("73e19507-bf40-44ce-822a-62630be49c2b"))
{driver.switchTo().window(winHandle);break;} // switch focus of WebDriver to the next found window handle (that's your newly opened window)
}
Log.info("Switched to new window");
}
catch(Exception e){
Log.error("Not able to switch the window --- " + e.getMessage());
DriverScript.bResult = false;
}
}
Seems that your browser is already closed.
I would simplified tests and not call quit anywhere in code.
I would also tried if problem exist in other browsers. Also U could debug or introduce sleeps to check where execly problem is.
I suspect there could be an issue with your code for switching windows. It would be better to debug if you could show the code for switching windows.