In the previous article, we have learned about the operations with Text Box, Radio Button, Drop Down.
This article we will learn about manipulating objects: check box, drag and drop, and keyboard operations.
I. Interact with checkboxes in Selenium
We can select a checkbox using the ‘click’ method and deselect by the ‘click’ method. Identify XPath using the ChroPath plugin on Chrome browser. This example uses the website http://www.calculator.net/mortgage-calculator.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package thao.automation; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class Checkbox { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "D:\SeleniumWebdriver\chromedriver.exe"); WebDriver driver = new ChromeDriver(); // Open website driver.get("http://www.calculator.net/mortgage-calculator.html"); // Maximize the browser driver.manage().window().maximize(); // Click on Radio Button driver.findElement(By.id("caddoptional")).click(); System.out.println("Is Selected: " + driver.findElement(By.id("caddoptional")).isSelected()); System.out.println("Is Enabled: " + driver.findElement(By.id("caddoptional")).isEnabled()); System.out.println("Is Displayed: " + driver.findElement(By.id("caddoptional")).isDisplayed()); // Click on other Radio Button driver.findElement(By.id("caddoptional")).click(); System.out.println("Is Selected " + driver.findElement(By.id("caddoptional")).isSelected()); // Close the Browser. driver.close(); } } |
Result:
Is Selected: false Is Enabled: true Is Displayed: true Is Selected true
II. Drag and drop in Selenium
We will perform drag and drop in Selenium by selecting a directory tree available on http://www.keenthemes.com/preview/metronic/templates/admin/ui_tree.html . In this example, we will drag an ‘Disable Node’ element from the ‘Initial Open’ folder to the ‘Parent Node’ folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package thao.automation; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Action; import org.openqa.selenium.interactions.Actions; public class DropDrag { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "D:\SeleniumWebdriver\chromedriver.exe"); WebDriver driver = new ChromeDriver(); // Open website driver.get("http://www.keenthemes.com/preview/metronic/templates" + "/admin/ui_tree.html"); // Maximize the browser driver.manage().window().maximize(); WebElement from = driver.findElement(By.xpath(".//a[@id='j3_7_anchor']")); WebElement to = driver.findElement(By.xpath(".//a[@id='j3_1_anchor']")); Actions builder = new Actions(driver); Action dragAndDrop = builder.clickAndHold(from) .moveToElement(to).release(to).build(); dragAndDrop.perform(); // driver.close(); } } |
Result:
III. Working with keyboards in selenium
We will perform keyboard operations in Selenium using the WebElement.sendKeys () or Actions.sendKeys () methods. Send keys to represent keyboard in browser. Special keys that are not text, are denoted by Keys that are identified as part of a single character string or character. The following is an example of performing a keyboard operation in Selenium to open https://google.com , enter the keyword ‘Son Tung MTP’ and press the Enter key to perform a search.
1. Use the Actions.sendKeys () method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package thao.automation; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; public class KeyBoardActionSendkeys { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "D:\SeleniumWebdriver\chromedriver.exe"); WebDriver driver = new ChromeDriver(); // Open website driver.get("https://google.com"); // Maximize the browser driver.manage().window().maximize(); WebElement searchElement = driver.findElement(By.name("q")); searchElement.sendKeys("Son Tung MTP"); Actions action = new Actions(driver); action.sendKeys(Keys.RETURN); action.perform(); } } |
Result:
2. Using the WebElement.sendKeys () method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package thao.automation; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class KeyBoardWebElementSendkeys { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "D:\SeleniumWebdriver\chromedriver.exe"); WebDriver driver = new ChromeDriver(); // Open website driver.get("https://google.com"); // Maximize the browser driver.manage().window().maximize(); WebElement searchElement = driver.findElement(By.name("q")); searchElement.sendKeys("Son Tung MTP", Keys.RETURN); } } |
So we have learned how to operate with Checkbox, drag and drop, and use the keyboard in Selenium. In the next article, we will learn about Mouse Actions, select multiple items and find all links in Selenium.