WHAT IS SENDKEYS METHOD?

sendKeys method is basically used to type some content into an editable field.



Sample code for sendKeys is:
driver.findElement(By.name("q")).sendkeys("Free Selenium Training");
//where drive is the already defined WebDriver instance




The above statement most of us already knows.

But do you know what happen if we execute the following code on Google Search home page, try it:
driver.findElement(By.name("q")).sendKeys("Free Selenium Training");
driver.findElement(By.name("q")).sendKeys("Free Selenium Training online");
//where driver is the already defined WebDriver instance

WebDriver enters the text "Free Selenium TrainingFree Selenium Training online" in search textbox.





To handle this situation you must use this code:
driver.findElement(By.name("q")).sendKeys("Free Selenium Training");
driver.findElement(By.name("q")).clear();
driver.findElement(By.name("q")).sendKeys("Free Selenium Training online");
//where driver is the already defined WebDriver instance

In the above code clear() method would make the textbox blank before entering next keyword.

So now sendkeys could be defined as: "sendKeys is method to pass the some content or text into an editable element without replacing the previous available content."



Post a Comment

0 Comments