- Selenium WebDriver 3 Practical Guide
- Unmesh Gundecha Satya Avasarala
- 272字
- 2021-08-13 16:08:11
The sendKeys() method
ThesendKeys action is applicable for textbox or textarea HTML elements. This is used to type text into the textbox. This will simulate the user keyboard and types text into WebElements exactly as a user would. The API syntax for the sendKeys() method is as follows:
void sendKeys(java.lang.CharSequence...keysToSend)
The input parameter for the preceding method is CharSequence of text that has to be entered into the element. This method doesn't return anything. Now, let's see a code example of how to type a search text into the Search box using the sendKeys() method:
@Test
public void elementSendKeysExample() {
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("Phones");
searchBox.submit();
assertThat(driver.getTitle())
.isEqualTo("Search results for: 'Phones'");
}
In the preceding code, the sendKeys() method is used to type the required text in the textbox element of the web page. This is how we deal with normal keys, but if you want to type in some special keys, such as Backspace, Enter, Tab, or Shift, we need to use a special enum class of WebDriver, named Keys. Using the Keys enumeration, you can simulate many special keys while typing into a WebElement.
Now let's see some code example, which uses the Shift key to type the text in uppercase in the Search Box:
@Test
public void elementSendKeysCompositeExample() {
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys(Keys.chord(Keys.SHIFT,"phones"));
searchBox.submit();
assertThat(driver.getTitle())
.isEqualTo("Search results for: 'PHONES'");
}
In the preceding code, the chord() method from the Keys enum is used to type the key, while the text specified is being given as an input to be the textbox. Try this in your environment to see all the text being typed in uppercase.
- GPS/GNSS原理與應用(第3版)
- 新一代物聯(lián)網架構技術:分層算力網絡
- 互聯(lián)網基礎資源技術與應用發(fā)展態(tài)勢(2021—2023)
- 互聯(lián)網安全的40個智慧洞見:2015年中國互聯(lián)網安全大會文集
- Building RESTful Web Services with Spring 5(Second Edition)
- Wireshark網絡分析就這么簡單
- 圖解手機元器件維修技巧
- Unity Artificial Intelligence Programming
- 基于性能的保障理論與方法
- Learning Windows 8 Game Development
- 數(shù)據血緣分析原理與實踐
- 云工廠:開啟中國制造云時代
- 數(shù)字王國里的虛擬人:技術、商業(yè)與法律解讀
- 工業(yè)以太網技術:AFDX/TTE網絡原理、接口、互連與安全
- Python Web Scraping Cookbook