Best Selenium code snippet using org.openqa.selenium.interactions.Actions.sendKeys
Source:action.java
...12 WebDriver driver = new ChromeDriver();13 driver.manage().window().maximize();14 driver.get("https://www.saucedemo.com/");15 ((JavascriptExecutor) driver).executeScript("scroll(0,300)");16 driver.findElement(By.id("user-name")).sendKeys("standard_user");17 driver.findElement(By.id("password")).sendKeys("secret_sauce");18 Thread.sleep(2000);19 driver.findElement(By.name("login-button")).click();20 Thread.sleep(2000);21 22 23 Actions ac = new Actions(driver);24 WebElement automate= driver.findElement(By.id("add-to-cart-sauce-labs-backpack"));25 automate.click();26 27 Thread.sleep(2000);28 driver.quit(); 29 }30}31Mouse Click32package action;33 34 import org.openqa.selenium.By;35 import org.openqa.selenium.Keys;36 import org.openqa.selenium.WebDriver;37 import org.openqa.selenium.WebElement;38 import org.openqa.selenium.chrome.ChromeDriver;39 import org.openqa.selenium.interactions.Actions;40 public class click {41 public static void main(String[] args) throws InterruptedException {42 43 System.setProperty("webdriver.chrome.driver","/usr/bin/chromedriver");44 WebDriver driver = new ChromeDriver();45 driver.get("https://www.amazon.in/");46 driver.manage().window().maximize();47 48 Actions action = new Actions(driver);49 WebElement elementToType = driver.findElement(By.xpath("//*[@id=\"twotabsearchtextbox\"]"));50 Thread.sleep(2000);51 action.sendKeys(elementToType, "watch").build().perform();52 Thread.sleep(2000);53 54 55 WebElement elementToClick = driver.findElement(By.xpath("//*[@id=\"nav-search-submit-button\"]"));56 Thread.sleep(2000);57 58 action.click(elementToClick).build().perform();59 Thread.sleep(2000);60 driver.quit();61 }62 }63 64 65Edureka Action class66 67package action;68import java.util.concurrent.TimeUnit;69import org.openqa.selenium.By;70import org.openqa.selenium.Keys;71import org.openqa.selenium.WebDriver;72import org.openqa.selenium.WebElement;73import org.openqa.selenium.chrome.ChromeDriver;74import org.openqa.selenium.interactions.Actions;75public class edurekaction {76 public static void main(String[] args) throws InterruptedException {77 System.setProperty("webdriver.chrome.driver","/usr/bin/chromedriver");78 WebDriver driver = new ChromeDriver();79 driver.get("https://www.edureka.co/");80 driver.manage().window().maximize();81 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);82 Actions action= new Actions(driver);83 action.moveToElement(driver.findElement(By.cssSelector("body:nth-child(2) "84 + "nav.navbar.navbar-default.nav_category:nth-child(9) > div.dropdown.dropdown_category_list"85 ))).build().perform();86 Thread.sleep(5000);87 WebElement element1=driver.findElement(By.cssSelector("nav.navbar.navbar-default.nav_category:nth-child(9)"88 + " div.dropdown.dropdown_category_list "89 + "ul.dropdown-menu.dropdown_menu_multi_level.hidden-xs.hidden-sm li.dropdown-submenu.dropdown_submenu_multi:nth-child(9) > "90 + "a.dropdown-toggle.ga_top_category"));91 92 action.moveToElement(element1).build().perform();93 Thread.sleep(2000);94 driver.findElement(By.xpath("//a[contains(text(),'Software Testing')]")).click();95 Thread.sleep(3000);96 driver.quit();97 }98 99}100Google search101package action;102import org.openqa.selenium.By;103import org.openqa.selenium.Keys;104import org.openqa.selenium.WebDriver;105import org.openqa.selenium.WebElement;106import org.openqa.selenium.chrome.ChromeDriver;107import org.openqa.selenium.interactions.Actions;108public class keydemo {109 public static void main(String[] args) throws InterruptedException {110 System.setProperty("webdriver.chrome.driver","/usr/bin/chromedriver");111 WebDriver driver = new ChromeDriver();112 driver.get("https://www.google.com/");113 driver.manage().window().maximize();114 WebElement element = driver.findElement(By.xpath("/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input"));115 116 Actions action = new Actions(driver);117 118 action.keyDown(element,Keys.SHIFT).sendKeys("swaglabs").build().perform();119 Thread.sleep(2000);120 121 driver.quit();122 123 }124}125Login Swag labs ActionClass126package action;127 import org.openqa.selenium.By;128 import org.openqa.selenium.Keys;129 import org.openqa.selenium.WebDriver;130 import org.openqa.selenium.WebElement;131 import org.openqa.selenium.chrome.ChromeDriver;132 import org.openqa.selenium.interactions.Action;133 import org.openqa.selenium.interactions.Actions;134 import org.openqa.selenium.interactions.ClickAction;135 public class logindemo {136 public static void main(String[] args) throws InterruptedException {137 System.setProperty("webdriver.chrome.driver","/usr/bin/chromedriver");138 WebDriver driver = new ChromeDriver();139 driver.get("https://www.saucedemo.com/");140 driver.manage().window().maximize();141 142 Actions builder = new Actions(driver);143 144 WebElement un = driver.findElement(By.id("user-name"));145 builder.moveToElement(un).build().perform();146 147 148 WebElement pass = driver.findElement(By.id("password"));149 builder.moveToElement(pass).build().perform();150 151 WebElement login =driver.findElement(By.id("login-button"));152 builder.moveToElement(login).build().perform();153 154 155 WebElement drop =driver.findElement(By.id("login-button"));156 builder.moveToElement(login).build().perform();157 Action SeriesOfActions;158 159 SeriesOfActions = (Action) builder160 161 .sendKeys(un,"standard_user")162 .sendKeys(pass,"secret_sauce")163 .keyDown(login, Keys.SHIFT)164 .keyUp(login, Keys.SHIFT)165 .build();166 SeriesOfActions.perform();167 168}169 }170 171 172Ebay search click173 174package action;175import org.openqa.selenium.By;176import org.openqa.selenium.Keys;177import org.openqa.selenium.WebDriver;178import org.openqa.selenium.WebElement;179import org.openqa.selenium.chrome.ChromeDriver;180import org.openqa.selenium.interactions.Actions;181public class ebayclick {182 public static void main(String[] args) throws InterruptedException {183 184 System.setProperty("webdriver.chrome.driver","/usr/bin/chromedriver");185 WebDriver driver = new ChromeDriver();186 driver.get("https://www.ebay.com/");187 driver.manage().window().maximize();188 189 Actions action = new Actions(driver);190 WebElement elementToType = driver.findElement(By.xpath("//*[@id=\"gh-ac\"]"));191 Thread.sleep(2000);192 action.sendKeys(elementToType, "watch").build().perform();193 Thread.sleep(2000);194 195 196 WebElement elementToClick = driver.findElement(By.xpath("//*[@id=\"gh-btn\"]"));197 Thread.sleep(2000);198 199 action.click(elementToClick).build().perform();200 Thread.sleep(2000);201 //driver.quit();202 }203}204Select Side Pannel205package action;206import java.util.concurrent.TimeUnit;207import org.openqa.selenium.By;208import org.openqa.selenium.WebDriver;209import org.openqa.selenium.WebElement;210import org.openqa.selenium.chrome.ChromeDriver;211import org.openqa.selenium.interactions.Actions;212public class swaglabsaction {213 public static void main(String[] args) throws InterruptedException {214 System.setProperty("webdriver.chrome.driver","/usr/bin/chromedriver");215 WebDriver driver = new ChromeDriver();216 driver.get("https://www.saucedemo.com/");217 driver.manage().window().maximize();218 driver.findElement(By.id("user-name")).sendKeys("standard_user");219 driver.findElement(By.id("password")).sendKeys("secret_sauce");220 Thread.sleep(2000);221 driver.findElement(By.name("login-button")).click();222 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);223 224 Actions action= new Actions(driver);225 action.moveToElement(driver.findElement(By.xpath("//*[@id=\"react-burger-menu-btn\"]"))).build().perform();226 WebElement elementToClick = driver.findElement(By.xpath("//*[@id=\"react-burger-menu-btn\"]"));227 action.click(elementToClick).build().perform();228 Thread.sleep(2000);229 driver.findElement(By.xpath("//*[@id=\"inventory_sidebar_link\"]")).click();230 Thread.sleep(3000);231 driver.quit();232 }233 ...
Source:GridKeyboardNavigationTest.java
...38 public void testSimpleKeyboardNavigation() {39 openTestURL();40 GridElement grid = getGridElement();41 grid.getCell(0, 0).click();42 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(ARROW_DOWN).perform();43 Assert.assertTrue("Body cell 1, 0 is not focused after keyboard navigation.", grid.getCell(1, 0).isFocused());44 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(ARROW_RIGHT).perform();45 Assert.assertTrue("Body cell 1, 1 is not focused after keyboard navigation.", grid.getCell(1, 1).isFocused());46 int i;47 for (i = 1; i < 40; ++i) {48 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(ARROW_DOWN).perform();49 }50 Assert.assertFalse("Grid has not scrolled with cell focus", isElementPresent(By.xpath("//td[text() = '(0, 0)']")));51 Assert.assertTrue("Cell focus is not visible", isElementPresent(By.xpath((("//td[text() = '(" + i) + ", 0)']"))));52 Assert.assertTrue((("Body cell " + i) + ", 1 is not focused"), grid.getCell(i, 1).isFocused());53 }54 @Test55 public void testNavigateFromHeaderToBody() {56 openTestURL();57 GridElement grid = getGridElement();58 grid.scrollToRow(300);59 new org.openqa.selenium.interactions.Actions(driver).moveToElement(grid.getHeaderCell(0, 7)).click().perform();60 grid.scrollToRow(280);61 Assert.assertTrue("Header cell is not focused.", grid.getHeaderCell(0, 7).isFocused());62 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(ARROW_DOWN).perform();63 Assert.assertTrue("Body cell 280, 7 is not focused", grid.getCell(280, 7).isFocused());64 }65 @Test66 public void testNavigationFromFooterToBody() {67 openTestURL();68 selectMenuPath("Component", "Footer", "Visible");69 GridElement grid = getGridElement();70 grid.scrollToRow(300);71 grid.getFooterCell(0, 2).click();72 Assert.assertTrue("Footer cell does not have focus.", grid.getFooterCell(0, 2).isFocused());73 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(ARROW_UP).perform();74 Assert.assertTrue("Body cell 300, 2 does not have focus.", grid.getCell(300, 2).isFocused());75 }76 @Test77 public void testNavigateBetweenHeaderAndBodyWithTab() {78 openTestURL();79 GridElement grid = getGridElement();80 grid.getCell(10, 2).click();81 Assert.assertTrue("Body cell 10, 2 does not have focus", grid.getCell(10, 2).isFocused());82 new org.openqa.selenium.interactions.Actions(getDriver()).keyDown(SHIFT).sendKeys(TAB).keyUp(SHIFT).perform();83 Assert.assertTrue("Header cell 0, 2 does not have focus", grid.getHeaderCell(0, 2).isFocused());84 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(TAB).perform();85 Assert.assertTrue("Body cell 10, 2 does not have focus", grid.getCell(10, 2).isFocused());86 // Navigate out of the Grid and try to navigate with arrow keys.87 new org.openqa.selenium.interactions.Actions(getDriver()).keyDown(SHIFT).sendKeys(TAB).sendKeys(TAB).keyUp(SHIFT).sendKeys(ARROW_DOWN).perform();88 Assert.assertTrue("Header cell 0, 2 does not have focus", grid.getHeaderCell(0, 2).isFocused());89 }90 @Test91 public void testNavigateBetweenFooterAndBodyWithTab() {92 openTestURL();93 selectMenuPath("Component", "Footer", "Visible");94 GridElement grid = getGridElement();95 grid.getCell(10, 2).click();96 Assert.assertTrue("Body cell 10, 2 does not have focus", grid.getCell(10, 2).isFocused());97 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(TAB).perform();98 Assert.assertTrue("Footer cell 0, 2 does not have focus", grid.getFooterCell(0, 2).isFocused());99 new org.openqa.selenium.interactions.Actions(getDriver()).keyDown(SHIFT).sendKeys(TAB).keyUp(SHIFT).perform();100 Assert.assertTrue("Body cell 10, 2 does not have focus", grid.getCell(10, 2).isFocused());101 // Navigate out of the Grid and try to navigate with arrow keys.102 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(TAB).sendKeys(TAB).sendKeys(ARROW_UP).perform();103 Assert.assertTrue("Footer cell 0, 2 does not have focus", grid.getFooterCell(0, 2).isFocused());104 }105 @Test106 public void testHomeEnd() throws Exception {107 openTestURL();108 getGridElement().getCell(100, 2).click();109 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(HOME).perform();110 Assert.assertTrue("First row is not visible", getGridElement().getCell(0, 2).isDisplayed());111 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(END).perform();112 Assert.assertTrue("Last row cell not visible", getGridElement().getCell(((GridBasicFeatures.ROWS) - 1), 2).isDisplayed());113 }114 @Test115 public void testPageUpPageDown() throws Exception {116 openTestURL();117 selectMenuPath("Component", "Size", "HeightMode Row");118 getGridElement().getCell(9, 2).click();119 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(PAGE_DOWN).perform();120 Assert.assertTrue("Row 17 did not become visible", isElementPresent(By.xpath("//td[text() = '(17, 2)']")));121 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(PAGE_DOWN).perform();122 Assert.assertTrue("Row 25 did not become visible", isElementPresent(By.xpath("//td[text() = '(25, 2)']")));123 checkFocusedCell(29, 2, 4);124 getGridElement().getCell(41, 2).click();125 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(PAGE_UP).perform();126 Assert.assertTrue("Row 33 did not become visible", isElementPresent(By.xpath("//td[text() = '(33, 2)']")));127 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(PAGE_UP).perform();128 Assert.assertTrue("Row 25 did not become visible", isElementPresent(By.xpath("//td[text() = '(25, 2)']")));129 checkFocusedCell(21, 2, 4);130 }131 @Test132 public void testNavigateOverHiddenColumnToFrozenColumn() {133 openTestURL();134 setFrozenColumns(3);135 toggleColumnHidden(1);136 getGridElement().getCell(0, 2).click();137 assertFocusedCell(0, 2);138 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(ARROW_LEFT).perform();139 assertFocusedCell(0, 1);140 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(ARROW_LEFT).perform();141 assertFocusedCell(0, 0);142 }143}...
Source:PageElementActions.java
...63 getActions().keyUp(getWebElement(element), theKey);64 return this;65 }66 /**67 * @see Actions#sendKeys(CharSequence...)68 */69 public PageElementActions sendKeys(CharSequence... keysToSend)70 {71 getActions().sendKeys(keysToSend);72 return this;73 }74 /**75 * @see Actions#sendKeys(org.openqa.selenium.WebElement, CharSequence...)76 */77 public PageElementActions sendKeys(PageElement element, CharSequence... keysToSend)78 {79 getActions().sendKeys(getWebElement(element), keysToSend);80 return this;81 }82 /**83 * @see org.openqa.selenium.interactions.Actions#clickAndHold(org.openqa.selenium.WebElement)84 */85 public PageElementActions clickAndHold(PageElement onElement)86 {87 getActions().clickAndHold(getWebElement(onElement));88 return this;89 }90 /**91 * @see org.openqa.selenium.interactions.Actions#clickAndHold()92 */93 public PageElementActions clickAndHold()...
Source:GridClientSelectionTest.java
...61 public void testDeselectAllowedKeyboardInput() {62 openTestURL();63 setSelectionModelSingle(true);64 getGridElement().getHeaderCell(0, 0).click();65 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(ARROW_DOWN).perform();66 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(SPACE).perform();67 Assert.assertTrue("Row 0 should be selected after pressing space", isRowSelected(0));68 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(ARROW_DOWN).perform();69 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(SPACE).perform();70 Assert.assertFalse("Row 0 should be deselected after pressing space another row", isRowSelected(0));71 Assert.assertTrue("Row 1 should be selected after pressing space", isRowSelected(1));72 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(SPACE).perform();73 Assert.assertFalse("Row should be deselected after pressing space again", isRowSelected(1));74 }75 @Test76 public void testDeselectNotAllowedMouseInput() {77 openTestURL();78 setSelectionModelSingle(false);79 getGridElement().getCell(5, 1).click();80 Assert.assertTrue("Row 5 should be selected after clicking", isRowSelected(5));81 getGridElement().getCell(7, 1).click();82 Assert.assertFalse("Row 5 should be deselected after clicking another row", isRowSelected(5));83 Assert.assertTrue("Row 7 should be selected after clicking", isRowSelected(7));84 getGridElement().getCell(7, 1).click();85 Assert.assertTrue("Row should remain selected after clicking again", isRowSelected(7));86 }87 @Test88 public void testDeselectNotAllowedKeyboardInput() {89 openTestURL();90 setSelectionModelSingle(false);91 getGridElement().getHeaderCell(0, 0).click();92 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(ARROW_DOWN).perform();93 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(SPACE).perform();94 Assert.assertTrue("Row 0 should be selected after pressing space", isRowSelected(0));95 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(ARROW_DOWN).perform();96 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(SPACE).perform();97 Assert.assertFalse("Row 0 should be deselected after pressing space another row", isRowSelected(0));98 Assert.assertTrue("Row 1 should be selected after pressing space", isRowSelected(1));99 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(SPACE).perform();100 Assert.assertTrue("Row should remain selected after pressing space again", isRowSelected(1));101 }102 @Test103 public void testChangeSelectionModelUpdatesUI() {104 openTestURL();105 setSelectionModelSingle(true);106 getGridElement().getCell(5, 1).click();107 Assert.assertTrue("Row 5 should be selected after clicking", isRowSelected(5));108 setSelectionModelNone();109 Assert.assertFalse("Row 5 should not be selected after changing selection model", isRowSelected(5));110 }111}...
Source:TreeGridHugeTreeNavigationTest.java
...15 @Test16 public void keyboard_navigation() {17 grid.getRow(0).getCell(0).click();18 // Should navigate to "Granddad 1" and expand it19 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(DOWN, RIGHT).perform();20 Assert.assertEquals(6, grid.getRowCount());21 assertCellTexts(0, 0, "Granddad 0", "Granddad 1", "Dad 1/0", "Dad 1/1", "Dad 1/2", "Granddad 2");22 checkRowFocused(1);23 // Should navigate to and expand "Dad 1/1"24 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(DOWN, DOWN, RIGHT).perform();25 assertCellTexts(0, 0, "Granddad 0", "Granddad 1", "Dad 1/0", "Dad 1/1", "Son 1/1/0", "Son 1/1/1", "Son 1/1/2", "Son 1/1/3");26 checkRowFocused(3);27 // Should navigate 100 items down28 Keys[] downKeyArr = new Keys[100];29 for (int i = 0; i < 100; i++) {30 downKeyArr[i] = Keys.DOWN;31 }32 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(downKeyArr).perform();33 WebElement son1_1_99 = findFocusedRow();34 Assert.assertEquals("Son 1/1/99 --", son1_1_99.getText());35 // Should navigate to "Dad 1/1" back36 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(HOME, DOWN, DOWN, DOWN).perform();37 WebElement dad1_1 = findFocusedRow();38 Assert.assertEquals("Dad 1/1 --", dad1_1.getText());39 // Should collapse "Dad 1/1"40 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(LEFT).perform();41 assertCellTexts(0, 0, "Granddad 0", "Granddad 1", "Dad 1/0", "Dad 1/1", "Dad 1/2", "Granddad 2");42 checkRowFocused(3);43 // Should navigate to "Granddad 1"44 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(LEFT).perform();45 assertCellTexts(0, 0, "Granddad 0", "Granddad 1", "Dad 1/0", "Dad 1/1", "Dad 1/2", "Granddad 2");46 checkRowFocused(1);47 // Should collapse "Granddad 1"48 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(LEFT).perform();49 assertCellTexts(0, 0, "Granddad 0", "Granddad 1", "Granddad 2");50 checkRowFocused(1);51 // Nothing should happen52 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(LEFT).perform();53 assertCellTexts(0, 0, "Granddad 0", "Granddad 1", "Granddad 2");54 checkRowFocused(1);55 assertNoErrorNotifications();56 }57 @Test58 public void no_exception_when_calling_expand_or_collapse_twice() {59 // Currently the collapsed state is updated in a round trip to the60 // server, thus it is possible to trigger an expand on the same row61 // multiple times through the UI. This should not cause exceptions, but62 // rather ignore the redundant calls.63 grid.getRow(0).getCell(0).click();64 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(RIGHT, RIGHT).perform();65 assertNoErrorNotifications();66 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(LEFT, LEFT).perform();67 assertNoErrorNotifications();68 }69 @Test70 public void uncollapsible_item() {71 grid.getRow(0).getCell(0).click();72 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(DOWN, DOWN, RIGHT).perform();73 grid.waitForVaadin();74 // expand Dad 2/175 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(DOWN, DOWN, RIGHT).perform();76 grid.waitForVaadin();77 assertNoErrorNotifications();78 assertCellTexts(5, 0, "Son 2/1/0");79 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(LEFT).perform();80 grid.waitForVaadin();81 assertNoErrorNotifications();82 assertCellTexts(5, 0, "Son 2/1/0");83 }84 @Test85 public void can_toggle_collapse_on_row_that_is_no_longer_in_cache() {86 grid.getRow(0).getCell(0).click();87 // Expand 2 levels88 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(RIGHT).perform();89 grid.waitForVaadin();90 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(DOWN, RIGHT).perform();91 grid.waitForVaadin();92 grid.scrollToRow(200);93 grid.waitForVaadin();94 // Jump into view95 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(LEFT).perform();96 grid.waitForVaadin();97 // Collapse98 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(LEFT).perform();99 grid.waitForVaadin();100 Assert.assertEquals(6, grid.getRowCount());101 // Expand102 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(RIGHT, UP).perform();103 grid.waitForVaadin();104 grid.scrollToRow(200);105 new org.openqa.selenium.interactions.Actions(getDriver()).sendKeys(RIGHT).perform();106 grid.waitForVaadin();107 Assert.assertEquals(306, grid.getRowCount());108 }109}
Source:MyActions.java
...51 public MyActions keyUp(WebElement element, Keys theKey) {52 action.addAction(new KeyUpAction(keyboard, mouse, (Locatable)element, theKey));53 return this;54 }55 public MyActions sendKeys(CharSequence keysToSend[]) {56 return sendKeys(null, keysToSend);57 }58 public MyActions sendKeys(WebElement element, CharSequence keysToSend[]) {59 action.addAction(new SendKeysAction(keyboard, mouse, (Locatable)element, keysToSend));60 return this;61 }62 public MyActions clickAndHold(WebElement onElement) {63 action.addAction(new ClickAndHoldAction(mouse, (Locatable)onElement));64 return this;65 }66 public MyActions clickAndHold() {67 return clickAndHold(null);68 }69 public MyActions release(WebElement onElement) {70 action.addAction(new ButtonReleaseAction(mouse, (Locatable)onElement));71 return this;72 }...
Source:Actions.java
...49 driver.manage().window().maximize(); WebElement menu=50 driver.findElement(By.linkText("actiTIME Inc."));51 org.openqa.selenium.interactions.Actions action=new52 org.openqa.selenium.interactions.Actions(driver);53 action.sendKeys(Keys.CONTROL).click(menu).perform(); Thread.sleep(1000);54 */5556// Context click57 /*WebDriver driver = new ChromeDriver();58 driver.get("http://demo.actitime.com");59 driver.manage().window().maximize();60 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);61 WebElement link = driver.findElement(By.linkText("actiTIME Inc."));62 org.openqa.selenium.interactions.Actions action = new org.openqa.selenium.interactions.Actions(driver);63 action.contextClick(link).perform();64 Robot r = new Robot();65 r.keyPress(KeyEvent.VK_T);66 */676869 // Drag and Drop7071 /*WebDriver driver=new ChromeDriver();72 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);73 driver.get("http://www.dhtmlgoodies.com/submitted-scripts/i-google-like-drag-drop/index.html");74 WebElement src= driver.findElement(By.xpath("//h1[.='Block 2']"));75 WebElement dest= driver.findElement(By.xpath("//h1[.='Block 1']"));76 org.openqa.selenium.interactions.Actions action=new org.openqa.selenium.interactions.Actions(driver);77 action.dragAndDrop(src, dest).perform();78*/ 79//Handling Frames80 /* WebDriver driver=new ChromeDriver();81 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);82 driver.get("https://jqueryui.com/droppable/");83 driver.switchTo().frame(0); 84 WebElement src= driver.findElement(By.id("draggable"));85 WebElement dest=driver.findElement(By.id("droppable"));86 org.openqa.selenium.interactions.Actions action=new org.openqa.selenium.interactions.Actions(driver);87 action.dragAndDrop(src, dest).perform();*/88 89//Handling frames 90 /* WebDriver driver=new ChromeDriver();91 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);92 driver.get("file:///C:/Users/mege/Desktop/page1.html");93 driver.switchTo().frame(0);94 driver.findElement(By.id("t2")).sendKeys("abc");95 driver.switchTo().parentFrame();96 driver.findElement(By.id("t1")).sendKeys("xyz");97*/9899//DoubleClick 100 WebDriver driver=new ChromeDriver();101 driver.get("https://drive.google.com/drive/my-drive");102 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);103 driver.findElement(By.id("identifierId")).sendKeys("niveditasd91@gmail.com");104 Robot r=new Robot();105 r.keyPress(KeyEvent.VK_ENTER);106 driver.findElement(By.name("password")).sendKeys("Megha@91");107 r.keyPress(KeyEvent.VK_ENTER);108 WebElement click = driver.findElement(By.className("l-u-Ab-zb-Pn-ve"));109 org.openqa.selenium.interactions.Actions action=new org.openqa.selenium.interactions.Actions(driver);110 action.doubleClick(click).perform();111 }112}
...
Source:KeyboardActions.java
...29 * Basic keyboard operations30 *31 * @return low level interface to control the keyboard32 * @deprecated Use {@link KeyboardActions#keyDown(Keys)} and {@link KeyboardActions#keyUp(Keys)}33 * and {@link KeyboardActions#sendKeys(CharSequence...)} instead34 */35 @Deprecated36 public Keyboard basic() {37 return ((HasInputDevices) driver).getKeyboard();38 }39 /**40 * Performs a modifier key press. Does not release the modifier key - subsequent interactions41 * may assume it's kept pressed.42 * Note that the modifier key is <b>never</b> released implicitly - either43 * <i>keyUp(theKey)</i> or <i>sendKeys(Keys.NULL)</i>44 * must be called to release the modifier.45 *46 * @param theKey Either {@link Keys#SHIFT}, {@link Keys#ALT} or {@link Keys#CONTROL}. If the47 * provided key is none of those, {@link IllegalArgumentException} is thrown.48 * @return this object reference to chain calls49 * @see org.openqa.selenium.interactions.Actions#keyDown(CharSequence)50 */51 public KeyboardActions keyDown(Keys theKey) {52 actions().keyDown(theKey).perform();53 return this;54 }55 /**56 * Performs a modifier key release. Releasing a non-depressed modifier key will yield undefined57 * behaviour.58 *59 * @param theKey Either {@link Keys#SHIFT}, {@link Keys#ALT} or {@link Keys#CONTROL}.60 * @return this object reference to chain calls61 * @see org.openqa.selenium.interactions.Actions#keyUp(CharSequence)62 */63 public KeyboardActions keyUp(Keys theKey) {64 actions().keyUp(theKey).perform();65 return this;66 }67 /**68 * Sends keys to the active element. This differs from calling69 * {@link WebElement#sendKeys(CharSequence...)} on the active element in two ways:70 * <ul>71 * <li>The modifier keys included in this call are not released.</li>72 * <li>There is no attempt to re-focus the element - so sendKeys(Keys.TAB) for switching73 * elements should work. </li>74 * </ul>75 *76 * @param keysToSend The keys.77 * @return A self reference.78 * @see org.openqa.selenium.interactions.Actions#sendKeys(CharSequence...)79 */80 public KeyboardActions sendKeys(CharSequence... keysToSend) {81 actions().sendKeys(keysToSend).perform();82 return this;83 }84}...
sendKeys
Using AI Code Generation
1import org.openqa.selenium.By;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.chrome.ChromeDriver;5import org.openqa.selenium.interactions.Actions;6public class SendKeysMethod {7 public static void main(String[] args) {8 System.setProperty("webdriver.chrome.driver", "C:\\Users\\Himanshu\\Downloads\\chromedriver_win32\\chromedriver.exe");9 WebDriver driver = new ChromeDriver();10 driver.manage().window().maximize();11 WebElement searchbox = driver.findElement(By.name("q"));12 Actions act = new Actions(driver);13 act.sendKeys(searchbox, "selenium").perform();14 }15}
sendKeys
Using AI Code Generation
1import org.openqa.selenium.By;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.firefox.FirefoxDriver;5import org.openqa.selenium.interactions.Actions;6public class SendKeysToElement {7 public static void main(String[] args) {8 WebDriver driver = new FirefoxDriver();9 WebElement searchBox = driver.findElement(By.name("q"));10 Actions builder = new Actions(driver);11 builder.sendKeys(searchBox, "Selenium WebDriver").perform();12 }13}
sendKeys
Using AI Code Generation
1import org.openqa.selenium.By;2import org.openqa.selenium.Keys;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.chrome.ChromeDriver;6import org.openqa.selenium.interactions.Actions;7public class sendKeysMethod {8 public static void main(String[] args) {9 System.setProperty("webdriver.chrome.driver", "C:\\Users\\Saurabh\\Desktop\\chromedriver.exe");10 WebDriver driver = new ChromeDriver();11 Actions act = new Actions(driver);12 driver.manage().window().maximize();13 WebElement search = driver.findElement(By.name("q"));14 act.sendKeys(search, "Selenium").perform();15 act.sendKeys(Keys.ENTER).perform();16 driver.close();17 }18}
sendKeys
Using AI Code Generation
1Actions obj = new Actions(driver);2obj.sendKeys(Keys.chord(Keys.CONTROL, "a")).perform();3obj.sendKeys(Keys.chord(Keys.CONTROL, "c")).perform();4Actions obj = new Actions(driver);5obj.sendKeys(Keys.chord(Keys.CONTROL, "a")).perform();6obj.sendKeys(Keys.chord(Keys.CONTROL, "x")).perform();7Actions obj = new Actions(driver);8obj.sendKeys(Keys.chord(Keys.CONTROL, "a")).perform();9obj.sendKeys(Keys.chord(Keys.CONTROL, "v")).perform();10Actions obj = new Actions(driver);11obj.sendKeys(Keys.chord(Keys.CONTROL, "a")).perform();12obj.sendKeys(Keys.chord(Keys.CONTROL, "z")).perform();13Actions obj = new Actions(driver);14obj.sendKeys(Keys.chord(Keys.CONTROL, "a")).perform();15obj.sendKeys(Keys.chord(Keys.CONTROL, "y")).perform();16Actions obj = new Actions(driver);17obj.sendKeys(Keys.chord(Keys.CONTROL, "a")).perform();18obj.sendKeys(Keys.chord(Keys.CONTROL, "f")).perform();19Actions obj = new Actions(driver);20obj.sendKeys(Keys.chord(Keys.CONTROL, "a")).perform();21obj.sendKeys(Keys.chord(Keys.CONTROL, "d")).perform();22Actions obj = new Actions(driver);23obj.sendKeys(Keys.chord(Keys.CONTROL, "a")).perform();24obj.sendKeys(Keys.chord(Keys.CONTROL, "g")).perform();25Actions obj = new Actions(driver);26obj.sendKeys(Keys.chord(Keys.CONTROL, "a")).perform();27obj.sendKeys(Keys.chord(Keys.CONTROL, "h")).perform();28Actions obj = new Actions(driver);29obj.sendKeys(Keys.chord(Keys.CONTROL, "a")).perform();30obj.sendKeys(Keys.chord(Keys.CONTROL, "i")).perform();
sendKeys
Using AI Code Generation
1Actions actions = new Actions(driver);2actions.sendKeys(Keys.TAB).perform();3driver.findElement(By.id("someId")).sendKeys(Keys.TAB);4driver.findElement(By.id("someId")).sendKeys(Keys.TAB);5Actions actions = new Actions(driver);6actions.sendKeys(Keys.TAB).perform();7driver.findElement(By.id("someId")).sendKeys(Keys.TAB);8driver.findElement(By.id("someId")).sendKeys(Keys.TAB);9Actions actions = new Actions(driver);10actions.sendKeys(Keys.TAB).perform();11driver.findElement(By.id("someId")).sendKeys(Keys.TAB);12driver.findElement(By.id("someId")).sendKeys(Keys.TAB);13Actions actions = new Actions(driver);14actions.sendKeys(Keys.TAB).perform();15driver.findElement(By.id("someId")).sendKeys(Keys.TAB);16driver.findElement(By.id("someId")).sendKeys(Keys.TAB);17Actions actions = new Actions(driver);18actions.sendKeys(Keys.TAB).perform();19driver.findElement(By.id("someId")).sendKeys(Keys.TAB);20driver.findElement(By.id("someId")).sendKeys(Keys.TAB);21Actions actions = new Actions(driver);22actions.sendKeys(Keys.TAB).perform();23driver.findElement(By.id("someId")).sendKeys(Keys.TAB);24driver.findElement(By.id("someId")).sendKeys(Keys.TAB);
sendKeys
Using AI Code Generation
1Actions actions = new Actions(driver);2actions.sendKeys(Keys.TAB).perform();3driver.findElement(By.id("someId")).sendKeys(Keys.TAB);4driver.findElement(By.id("someId")).sendKeys(Keys.TAB);5Actions actions = new Actions(driver);6actions.sendKeys(Keys.TAB).perform();7driver.findElement(By.id("someId")).sendKeys(Keys.TAB);8driver.findElement(By.id("someId")).sendKeys(Keys.TAB);9Actions actions = new Actions(driver);10actions.sendKeys(Keys.TAB).perform();11driver.findElement(By.id("someId")).sendKeys(Keys.TAB);12driver.findElement(By.id("someId")).sendKeys(Keys.TAB);13Actions actions = new Actions(driver);14actions.sendKeys(Keys.TAB).perform();15driver.findElement(By.id("someId")).sendKeys(Keys.TAB);16driver.findElement(By.id("someId")).sendKeys(Keys.TAB);17Actions actions = new Actions(driver);18actions.sendKeys(Keys.TAB).perform();19driver.findElement(By.id("someId")).sendKeys(Keys.TAB);20driver.findElement(By.id("someId")).sendKeys(Keys.TAB);21Actions actions = new Actions(driver);22actions.sendKeys(Keys.TAB).perform();23driver.findElement(By.id("someId")).sendKeys(Keys.TAB);24driver.findElement(By.id("someId")).sendKeys(Keys.TAB);
sendKeys
Using AI Code Generation
1import java.util.concurrent.TimeUnit;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.firefox.FirefoxDriver;4import org.openqa.selenium.interactions.Actions;5import org.openqa.selenium.By;6import org.openqa.selenium.WebElement;7public class SendKeysToActiveElement {8public static void main(String[] args) {9WebDriver driver = new FirefoxDriver();10driver.manage().timeouts().implicitlyWait(10n TimeUnit.SECONDS);11Actions act = new Actions(driver);12act.sendKeys(aselenium").perform();13WebElement searchButton = driver.findElement(By.name(sbtnK"elenium.Keys class14searchButton.click();15driver.close();16}17}18Related posts: How to send keys to the active element using Robot class in Selenium WebDriver? How to use sendKeys method in Selenium WebDriver? How to use sendKeys method of Actions class in Selenium WebDriver? How to use sendKeys method of WebElement class in Selenium WebDriver? How to use sendKeys method of WebElement class in Selenium WebDriver?iver.findElement(By.id("txtUsername")).sendKeys(Keys.chord(Keys.CONTROL, "a"));19Actions actions = new Actions(driver);20actions.sendKeys(Keys.chord(Keys.CONTROL, "a"));21driver.findElement(By.id("txtUsername")).sendKeys(Keys.chord(Keys.CONTROL, "a"));22Actions actions = new Actions(driver);23actions.sendKeys(Keys.chord(Keys.CONTROL, "a"));24driver.findElement(By.id("txtUsername")).sendKeys(Keys.chord(Keys.CONTROL, "a"));25Actions actions = new Actions(driver);26actions.sendKeys(Keys.chord(Keys.CONTROL, "a"));27driver.findElement(By.id("txtUsername")).sendKeys(Keys.chord(Keys.CONTROL, "a"));28Actions actions = new Actions(driver);29actions.sendKeys(Keys.chord(Keys.CONTROL, "a"));30driver.findElement(By.id("txtUsername")).sendKeys(Keys.chord(Keys.CONTROL, "a"));31Actions actions = new Actions(driver);32actions.sendKeys(Keys.chord(Keys.CONTROL, "a"));33driver.findElement(By.id("txtUsername")).sendKeys(Keys.chord(Keys.CONTROL, "a"));34Actions actions = new Actions(driver);35actions.sendKeys(Keys.chord(Keys.CONTROL, "a"));
sendKeys
Using AI Code Generation
1import java.util.concurrent.TimeUnit;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.firefox.FirefoxDriver;4import org.openqa.selenium.interactions.Actions;5import org.openqa.selenium.By;6import org.openqa.selenium.WebElement;7public class SendKeysToActiveElement {8public static void main(String[] args) {9WebDriver driver = new FirefoxDriver();10driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);11Actions act = new Actions(driver);12act.sendKeys("selenium").perform();13WebElement searchButton = driver.findElement(By.name("btnK"));14searchButton.click();15driver.close();16}17}
LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.
Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.
What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.
Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.
Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.
How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.
Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.
Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.
LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!