11/23/17

Double Click Action In Selenium WebDriver


In some scenarios, we may need to do double click action on a particular element to move further. In such cases, we use Actions class in Selenium WebDriver to work on Mouse and Keyboard Actions. Check out the below code for detailed explanation of Actions Class.


Scenario to be automated:
  1. Launch the web browser and open the application
  2. Find the required element and do double click on the element
  3. Close the browser to end the program
Copy the below mentioned script and work on this scenario.
import org.openqa.selenium.By;import org.openqa.selenium.JavascriptExecutor;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.interactions.Actions;
public class DoubleClick {
public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver", "C://Drivers//chromedriver_win32//chromedriver.exe"); WebDriver driver = new ChromeDriver();
driver.get("http://api.jquery.com/dblclick/");
Actions act = new Actions(driver);
driver.switchTo().frame(0);
WebElement ele = driver.findElement(By.xpath("/html/body/div"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", ele);// scoll down for the element
String color = ele.getCssValue("color").toString(); System.out.println("color before double click:" + color); // blue
Thread.sleep(2000); act.doubleClick(ele).build().perform();
Thread.sleep(2000); color = ele.getCssValue("color").toString(); System.out.println("color after double click:" + color); // yellow
}
}

0 comments:

Post a Comment

Google Q&A Forum