Best Selenium code snippet using org.openqa.selenium.interactions.Interface Keyboard.sendKeys
Source:WebDriverEventListener.java
...1013 */1014 void afterGetRect(Step step, Rectangle rectangle, WebElement element);10151016 /**1017 * Called before {@link WebElement#sendKeys WebElement.sendKeys(...)}.1018 * @param step1019 * step record1020 * @param element1021 * the WebElement being used for the action1022 * @param keysToSend1023 * text to insert1024 */1025 void beforeSendKeysByElement(Step step, WebElement element, CharSequence... keysToSend);10261027 /**1028 * Called after {@link WebElement#sendKeys WebElement.sendKeys(...)}}. Not called, if an1029 * exception is thrown.1030 * @param step1031 * step record1032 * @param element1033 * the WebElement being used for the action1034 * @param keysToSend1035 * text to insert1036 */1037 void afterSendKeysByElement(Step step, WebElement element, CharSequence... keysToSend);1038 1039 /**1040 * Called before {@link WebElement#submit WebElement.submit()}.1041 * @param step1042 * step record1043 * @param element1044 * the WebElement being used for the action1045 */1046 void beforeSubmit(Step step, WebElement element);10471048 /**1049 * Called after {@link WebElement#submit WebElement.submit()}. Not called, if an1050 * exception is thrown.1051 * @param step1052 * step record1053 * @param element1054 * the WebElement being used for the action1055 */1056 void afterSubmit(Step step, WebElement element);10571058 /**1059 * Called before {@link org.openqa.selenium.interactions.Keyboard#sendKeys Keyboard.sendKeys(...)}.1060 * @param step1061 * step record1062 * @param keysToSend1063 * text to insert1064 */1065 void beforeSendKeysByKeyboard(Step step, CharSequence... keysToSend);10661067 /**1068 * Called after {@link org.openqa.selenium.interactions.Keyboard#sendKeys Keyboard.sendKeys(...)}}. Not called, if an1069 * exception is thrown.1070 * @param step1071 * step record1072 * @param keysToSend1073 * text to insert1074 */1075 void afterSendKeysByKeyboard(Step step, CharSequence... keysToSend);10761077 /**1078 * Called before {@link org.openqa.selenium.interactions.Keyboard#pressKey Keyboard.pressKey(...)}.1079 * @param step1080 * step record1081 * @param keyToPress1082 * key to press
...
Source:SeleniumHelper.java
...42 Actions a=new Actions(dr);43 public void handleMultipleWindow()44 {45 String pwin=dr.getWindowHandle();46 a.contextClick(element).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).perform();47 windows=dr.getWindowHandles();48 for(String win:windows)49 {50 if(!win.equals(pwin))51 {52 dr.switchTo().window(win);53 }54 }55 56 }57 58 59 60 public void multipleframe(String value)61 {62 elements=dr.findElements(By.tagName("iframe"));63 for(WebElement ele:elements)64 {65 if(ele.getAttribute("id").equals(value))66 {67 dr.switchTo().frame(value);68 }69 }70 }71 72 public void getLinkText(String value)73 {74 elements=dr.findElements(By.tagName("a"));75 for(WebElement ele:elements)76 {77 String t=ele.getText();78 if(t.equals(value))79 {80 ele.click();81 }82 }83 }84 85 86 87 88 89 @Test90 public void test1()91 {92 //Method from driver93 dr.close();//to close focus window94 dr.quit();//to close whole instance95 dr.get("");//go to url96 element=dr.findElement(By.id(""));//to find any element97 elements=dr.findElements(By.id(""));//to find more than one element98 text=dr.getPageSource();//to get source code of html page99 text=dr.getTitle();//to get the title100 text=dr.getCurrentUrl();//to get the current url of page101 text=dr.getWindowHandle();//to get parent window102 windows=dr.getWindowHandles();//to get all child window103 option=dr.manage();104 nevigate=dr.navigate();105 locate=dr.switchTo();106 107 108 //get() vs nevigate().to();109 110 //method from Webelement111 element.clear();//to clear text box112 element.click();//to click on element113 element.findElement(By.id(""));//to find an element under another element114 element.getAttribute("id");//to get the attribute value115 element.getCssValue("");//to get the cssValue116 element.getLocation().getX();//to get x coordinate size117 element.getLocation().getY();//to get y coordinate size118 File f=element.getScreenshotAs(OutputType.FILE);//to take screen shot119 element.getTagName();//to know the tag name to create particuler element120 element.getText();//to get the text from element121 element.getSize();////to get the size of Dimension122 result=element.isDisplayed();//to check element is present or not in webPage123 result=element.isEnabled();//to check element is enabled or not124 result=element.isSelected();//to check checkbox is selected or125 element.sendKeys("");//to type on text box126 element.submit();//to click on element without click127 128 //method from Select129 Select s=new Select(element);130 s.selectByIndex(0);//to get drop down option by index131 s.selectByValue("");//to get drop down option by text from DOM132 s.selectByVisibleText("");//to get drop down option by visible text value from UI133 s.getFirstSelectedOption();//to get first option from drop down without parameter134 elements=s.getOptions();135 elements.size();136 elements=s.getAllSelectedOptions();137 //note from select class any method u have for select there are all methods for deselect also138 139 //method from dr.switchTo();140 Alert alert=dr.switchTo().alert();141 alert.accept();142 alert.dismiss();143 alert.getText();144 alert.sendKeys("ngf");145 146 /*Automation challenge147 if we see customized button in alert box like done dismiss etc selenium cannot148 handle this for that we need third party API called Sikulli and Robot Class ,149 this two api can handle any desktop 150 base application and viseo as well.*/151 152 //iframe153 dr.switchTo().frame(0);154 dr.switchTo().frame("");155 dr.switchTo().frame(element);156 dr.switchTo().defaultContent();157 dr.switchTo().parentFrame();158 159 //Automation challenge160 //To handle multiple frame is not easy161 162 163 //method from Actions164 165 a.click();//to click166 a.click(element);//to click on element167 a.clickAndHold();168 a.clickAndHold(element);169 a.contextClick();//right click170 a.contextClick(element);//right click on element171 a.doubleClick();//double click172 a.dragAndDrop(element, element).build().perform();;173 a.dragAndDropBy(element, 200, 500).build().perform();174 a.keyDown(Keys.ARROW_DOWN);//scroll down175 a.moveToElement(element).build().perform();//hover over176 a.sendKeys(Keys.ENTER);//to click from Enter button from keyboard177 178 //method from dr.nevigate()179 dr.navigate().back();180 dr.navigate().forward();181 dr.navigate().refresh();182 dr.navigate().to("http://www.google.com");183 184 185 186 //methods from dr.manage()187 //to handle cookies of webPage188 Cookie c=dr.manage().getCookieNamed("");189 dr.manage().addCookie(c);190 Set<Cookie> cookies=dr.manage().getCookies();191 for(Cookie c1:cookies)192 {193 c1.getValue();194 }195 dr.manage().deleteCookie(c);196 dr.manage().deleteAllCookies();197 198 //window specific199 dr.manage().window().fullscreen();200 dr.manage().window().maximize();201 dr.manage().window().getSize();202 203 dr.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);//its for204 //whole UI and it wait upto given time205 206 207 //Method from JavascriptExecutor,ScrrenShot,KeyBoard,Keys,HasInputDevices,Coordinates208 JavascriptExecutor js=(JavascriptExecutor)dr;209 js.executeScript("window.scrollBy(100, 0)");210 js.executeScript("document.getElementById('email').click()");211 js.executeScript("arguments[0].value='jghj'",dr.findElement(By.id("")));212 // JavascriptExecutor is an interface using this we can perform all action which we can213 //do using selenium 214 215 File f1=((TakesScreenshot)dr).getScreenshotAs(OutputType.FILE);216 217 //Coordinates co=((Locatable)element).getCoordinates();218 //co.inViewPort();219 220 Keyboard k=((HasInputDevices)dr).getKeyboard();221 k.sendKeys(Keys.chord(Keys.COMMAND,Keys.SHIFT,"p"));222 223 //ExplicitWait224// its for particular element . it is logical wait225// webdriverwait and fluentwait is the example of explicit wait.226// Using Expected conditions we can set different methods for logical wait like visibilityOf elementtobeclickable etc.227// waitForElement(element).click();228 229 230 231 }232 233 public WebElement waitForElement(WebElement element)234 {235 WebDriverWait wait=new WebDriverWait(dr, 10);...
Source:KeyboardElementActions.java
...43 * Basic keyboard operations44 *45 * @return low level interface to control the keyboard46 * @deprecated Use {@link KeyboardActions#keyDown(Keys)} and {@link KeyboardActions#keyUp(Keys)}47 * and {@link KeyboardActions#sendKeys(CharSequence...)} instead48 */49 @Deprecated50 public Keyboard basic() {51 return ((HasInputDevices) driver).getKeyboard();52 }53 /**54 * Performs a modifier key press after focusing on an element. Equivalent to:55 * <i>Actions.click(element).sendKeys(theKey);</i>56 *57 * @param theKey Either {@link Keys#SHIFT}, {@link Keys#ALT} or {@link Keys#CONTROL}. If the58 * provided key is none of those, {@link IllegalArgumentException} is thrown.59 * @return this object reference to chain calls60 * @see #keyDown(org.openqa.selenium.Keys)61 * @see org.openqa.selenium.interactions.Actions#keyDown(WebElement, CharSequence)62 */63 public KeyboardElementActions keyDown(Keys theKey) {64 actions().keyDown(element, theKey).perform();65 return this;66 }67 /**68 * Performs a modifier key release after focusing on an element. Equivalent to:69 * <i>Actions.click(element).sendKeys(theKey);</i>70 *71 * @param theKey Either {@link Keys#SHIFT}, {@link Keys#ALT} or {@link Keys#CONTROL}.72 * @return this object reference to chain calls73 * @see org.openqa.selenium.interactions.Actions#keyUp(WebElement, CharSequence)74 */75 public KeyboardElementActions keyUp(Keys theKey) {76 actions().keyUp(element, theKey).perform();77 return this;78 }79 /**80 * Sends keys to the active element. This differs from calling81 * {@link WebElement#sendKeys(CharSequence...)} on the active element in two ways:82 * <ul>83 * <li>The modifier keys included in this call are not released.</li>84 * <li>There is no attempt to re-focus the element - so sendKeys(Keys.TAB) for switching85 * elements should work. </li>86 * </ul>87 *88 * @param keysToSend The keys.89 * @return this object reference to chain calls90 * @see org.openqa.selenium.interactions.Actions#sendKeys(WebElement, CharSequence...)91 */92 public KeyboardElementActions sendKeys(CharSequence... keysToSend) {93 actions().sendKeys(element, keysToSend).perform();94 return this;95 }96}...
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}...
Source:KeyboardSeriesOfAction.java
...4546 //Method 1 - Using Action Class47 /* action.moveToElement(searchBox)48 .keyDown(searchBox, Keys.SHIFT)49 .sendKeys(searchBox, value)50 .keyUp(searchBox, Keys.SHIFT)51 .pause(3000)52 .sendKeys(Keys.ENTER)53 .perform(); */5455 //Method 2 - Using Action Interface56 Action seriesOfAction = action.moveToElement(searchBox)57 .keyDown(searchBox, Keys.SHIFT)58 .sendKeys(searchBox, value)59 .keyUp(searchBox, Keys.SHIFT)60 .sendKeys(Keys.ENTER)61 .build();6263 seriesOfAction.perform();6465 Thread.sleep(3000);6667 //Close the Application68 myDriverInstance.close();69 System.out.println("Application is closed");7071727374
...
Source:PlatformBasedActions.java
...24 public PlatformBasedActions(WebDriver driver) {25 super(driver);26 }27 @Override28 public Actions sendKeys(WebElement element, CharSequence... keysToSend) {29 action.addAction(30 new SendKeysAction(keyboard, mouse, (Locatable) element, modifyCharSequence(keysToSend)));31 return this;32 }33 protected abstract CharSequence[] modifyCharSequence(CharSequence... keysToSend);34}...
Source:KeyBoard_interFaceClass.java
...28 WebElement Exp_salary=driver.findElement(By.xpath("//input[@id='cjaMinSal']"));29 Exp_salary.click();30 Thread.sleep(5000);31 32 key.sendKeys(Keys.ARROW_DOWN,Keys.ARROW_DOWN,Keys.ARROW_DOWN);33 Thread.sleep(3000);34 key.sendKeys(Keys.ENTER);35 36 37 38 3940 }4142}
...
Source:keyboardMouse.java
...19 Action seriesOfActions = actions20 .moveToElement(element)21 .click()22 .keyDown(element,Keys.SHIFT)23 .sendKeys("monika")24 .keyUp(element,Keys.SHIFT)25 .doubleClick(element)26 .contextClick()27 .build();28 seriesOfActions.perform();29 30 //---------------------OR------------31 32 actions.moveToElement(element).contextClick().build().perform();33 34 35 }36}...
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.firefox.FirefoxDriver;6public class SendKeysExample {7 public static void main(String[] args) {8 WebDriver driver = new FirefoxDriver();9 WebElement searchBox = driver.findElement(By.name("q"));10 searchBox.sendKeys("webdriver");11 searchBox.sendKeys(Keys.RETURN);12 driver.quit();13 }14}
sendKeys
Using AI Code Generation
1import org.openqa.selenium.Keys;2driver.findElement(By.id("search")).sendKeys(Keys.ENTER);3driver.findElement(By.id("search")).sendKeys(Keys.CONTROL + "a");4driver.findElement(By.id("search")).sendKeys(Keys.CONTROL + "c");5import org.openqa.selenium.Keys;6driver.findElement(By.id("search")).sendKeys(Keys.ENTER);7driver.findElement(By.id("search")).sendKeys(Keys.CONTROL + "a");8driver.findElement(By.id("search")).sendKeys(Keys.CONTROL + "c");9driver.findElement(By.id("search")).sendKeys(Keys.CONTROL + "v");10import org.openqa.selenium.Keys;11driver.findElement(By.id("search")).sendKeys(Keys.ENTER);12driver.findElement(By.id("search")).sendKeys(Keys.CONTROL + "a");13driver.findElement(By.id("search")).sendKeys(Keys.CONTROL + "c");14driver.findElement(By.id("search")).sendKeys(Keys.CONTROL + "v");15driver.findElement(By.id("search")).sendKeys(Keys.DELETE);16import org.openqa.selenium.Keys;17driver.findElement(By.id("search")).sendKeys(Keys.ENTER);18driver.findElement(By.id("search")).sendKeys(Keys.CONTROL + "a");19driver.findElement(By.id("search")).sendKeys(Keys.CONTROL + "c");20driver.findElement(By.id("search")).sendKeys(Keys.CONTROL + "v");21driver.findElement(By.id("search")).sendKeys(Keys.DELETE);22driver.findElement(By.id("search")).sendKeys(Keys.CONTROL + "v");23import org.openqa.selenium.Keys;24driver.findElement(By.id("search")).sendKeys(Keys.ENTER);25driver.findElement(By.id("search")).sendKeys(Keys.CONTROL + "a");26driver.findElement(By.id("search")).sendKeys(Keys.CONTROL + "c");27driver.findElement(By.id("search")).sendKeys(Keys.CONTROL + "v");28driver.findElement(By.id("search")).sendKeys(Keys.DELETE);29driver.findElement(By.id("search")).sendKeys(Keys.CONTROL + "v");30driver.findElement(By.id("search")).sendKeys(Keys.CONTROL + "a");31driver.findElement(By.id("search")).sendKeys(Keys.CONTROL + "v");32import org.openqa.selenium.Keys;33driver.findElement(By.id("search")).sendKeys(Keys.ENTER);34driver.findElement(By.id("search")).sendKeys
sendKeys
Using AI Code Generation
1import org.openqa.selenium.WebDriver;2import org.openqa.selenium.firefox.FirefoxDriver;3import org.openqa.selenium.By;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.interactions.Actions;6import org.openqa.selenium.interactions.Keyboard;7import org.openqa.selenium.Keys;8public class SendKeysExample {9 public static void main(String[] args) {10 WebDriver driver = new FirefoxDriver();11 WebElement element = driver.findElement(By.name("q"));12 Actions builder = new Actions(driver);13 Keyboard keyboard = builder.getKeyboard();14 keyboard.sendKeys(element, "Selenium");15 keyboard.sendKeys(element, Keys.ENTER);16 driver.quit();17 }18}19import org.openqa.selenium.WebDriver;20import org.openqa.selenium.firefox.FirefoxDriver;21import org.openqa.selenium.By;22import org.openqa.selenium.WebElement;23import org.openqa.selenium.interactions.Actions;24import org.openqa.selenium.interactions.Keyboard;25import org.openqa.selenium.Keys;26public class SendKeysExample {27 public static void main(String[] args) {28 WebDriver driver = new FirefoxDriver();29 driver.get("
sendKeys
Using AI Code Generation
1Keyboard keyboard = ((HasInputDevices) driver).getKeyboard();2keyboard.sendKeys("abc");3keyboard.sendKeys(Keys.F5);4Mouse mouse = ((HasInputDevices) driver).getMouse();5mouse.click(null);6Keyboard keyboard = ((HasInputDevices) driver).getKeyboard();7keyboard.sendKeys("abc");8keyboard.sendKeys(Keys.F5);9Mouse mouse = ((HasInputDevices) driver).getMouse();10mouse.click(null);11Keyboard keyboard = ((HasInputDevices) driver).getKeyboard();12keyboard.sendKeys("abc");13keyboard.sendKeys(Keys.F5);14Mouse mouse = ((HasInputDevices) driver).getMouse();15mouse.click(null);16Keyboard keyboard = ((HasInputDevices) driver).getKeyboard();17keyboard.sendKeys("abc");18keyboard.sendKeys(Keys.F5);19Mouse mouse = ((HasInputDevices) driver).getMouse();20mouse.click(null);
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!!