Selenium automated testing often uses keyboard operations, which are the detailed operation of the keyboard, and part of the code. From the worm division of Automation-related books.
public static void Main (string[] args) throws Interruptedexception {
System.setproperty ("Webdriver.chrome.driver", "D:/chromedriver_win32/chromedriver.exe");
Chromeoptions Options = new Chromeoptions ();
Options.addarguments ("User-data-dir=c:\\users\\happy\\appdata\\local\\google\\chrome\\user data");
Webdriver Driver = new Chromedriver (Options);
try {
Driver.get ("https://www.baidu.com");
Webelement target = driver.findelement (by.id ("kw"));
Target.sendkeys ("I am an automated script");
Enter information in the Baidu text box
Thread.Sleep (3000);
Target.sendkeys (Keys.back_space);
Delete a character after
Thread.Sleep (3000);
Target.clear ();
Clear the contents of the text box
Thread.Sleep (3000);
Target.sendkeys (Keys.space);
Enter a space
Thread.Sleep (3000);
Target.sendkeys ("I am an automated script");
Re-enter
Thread.Sleep (3000);
Target.sendkeys (Keys.control, "a");
Select All
Thread.Sleep (3000);
Target.sendkeys (Keys.control, "X");
Shear
Thread.Sleep (3000);
Target.sendkeys (Keys.control, "V");
Paste
Thread.Sleep (3000);
Target.sendkeys (Keys.enter);
Click Enter. Typically used for login scenarios
Thread.Sleep (5000);
} finally {
try {
Driver.close ();
Runtime.getruntime (). EXEC ("taskkill/f/im" + "Chromedriver.exe");
Runtime.getruntime (). EXEC ("taskkill/f/im" + "Chrome.exe");
End the process. The above is used to end the Chromedriver process when an exception occurs and to avoid chromedriver consuming memory resources. Close (), and the Quit () method sometimes fail to end the chromedriver process.
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
Selenium 3.0 Keyboard Event + Force end Chromedriver Process Code