How to pass keys from keyboard in selenium using C#

During the automation, some time we need to pass keys from keyboard in selenium using C#. Like select text (Ctrl+A), Copy Text (Ctrl+V), Paste Text (Ctrl + V). So how to pass keyboard values in selenium using c#. In this article I am going to cover some example which is often used during the automation.

Selenium web driver lib provide Keys enum to handle all the non text keys. Let’s see all the example one by one.

Pass keys from keyboard in selenium using C#

How to pass Ctrl + A command in selenium C# from Keyboard.

In this example, We are going to pass Keys.Control + “a” to select text in a TextBox.

IWebElement TextBox = Driver.FindElement(By.XPath("Element_Xapth"));
            TextBox.SendKeys(Keys.Control + "a");

How to press backspace button in selenium C# from Keyboard.

In this example, We are going to press backspace button.

IWebElement TextBox = Driver.FindElement(By.XPath("Element_Xapth"));
            TextBox.SendKeys(Keys.Backspace);

How to pass Ctrl + C command in selenium C# from Keyboard.

In this example, We are going to pass Keys.Control + “c” to copy text in a TextBox.

IWebElement TextBox = Driver.FindElement(By.XPath("Element_Xapth"));
          TextBox.SendKeys(Keys.Control + "c");

How to pass Ctrl + V command in selenium C# from Keyboard.

 In this example, We are going to pass Keys.Control + “v” to paste text in a TextBox.

IWebElement TextBox = Driver.FindElement(By.XPath("Element_Xapth"));
          TextBox.SendKeys(Keys.Control + "v");

How to press Delete button in selenium C# from Keyboard.

In this example, We are going to press Delete button.

IWebElement TextBox = Driver.FindElement(By.XPath("Element_Xapth"));
            TextBox.SendKeys(Keys.Delete);

To find more command please Switch to visual studio enter a Keys with (.) the suggestion box will help you find the more commands.

pass keys from keyboard in selenium using C#

 

Hers is the list of Keys With selenium code

Arrow Down :    TextBox.SendKeys(Keys.ArrowDown);

Arrow UP:          TextBox.SendKeys(Keys.ArrowUp);

ArrowLeft:         TextBox.SendKeys(Keys.ArrowLeft);

ArrowRight:      TextBox.SendKeys(Keys.ArrowRight);

Enter:                 TextBox.SendKeys(Keys.Enter);

Escape:               TextBox.SendKeys(Keys.Escape);

Function Key F2      TextBox.SendKeys(Keys.F2);

Function Key F3      TextBox.SendKeys(Keys.F3);

Function Key F4      TextBox.SendKeys(Keys.F4);

Function Key F5      TextBox.SendKeys(Keys.F5);

Function Key F6      TextBox.SendKeys(Keys.F6);

Function Key F7      TextBox.SendKeys(Keys.F7);

Function Key F8      TextBox.SendKeys(Keys.F8);

Function Key F9      TextBox.SendKeys(Keys.F9);

Function Key F10 TextBox.SendKeys(Keys.F10);

Function Key F11 TextBox.SendKeys(Keys.F11);

Function Key F12 TextBox.SendKeys(Keys.F12);

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *

1 + fifteen =