Resolved Selenium C# - Sendkeys method not entering ^~ characters

Palak Shah

Well-known member
Joined
Apr 29, 2020
Messages
88
Programming Experience
1-3
Hello Everyone,

I have one text which I want to pass in input box, but it is not entering special characters and adding space instead of that - can someone help me in that?

Text which I want to enter in the input box: @"This is a ^~ in C#.";

I tried below thing:

string demo = @"This is a ^~ in C#."; Driver.SendKeys(element, demo);

But it enters below text:

This is a in C#.

Send Keys Code:
public static void SendKeys(this IWebDriver Driver, IWebElement element, String keys, bool shouldClickOnElementBeforeSendKeys = true, bool shouldCheckValueAfterSendKeys = true, int timeout = SeleniumExtensionMembers.defaultTimeout)
        {
            ThreadHelper.VoidWait(() =>
            {
                Driver.SendKeysFlowDecider(element, keys, shouldClickOnElementBeforeSendKeys, shouldCheckValueAfterSendKeys, timeout);
            }, true, timeout);

        }
        public static void SendKeysFlowDecider(this IWebDriver Driver, IWebElement element, String keys, bool shouldClickOnElementBeforeSendKeys = true, bool shouldCheckValueAfterSendKeys = true, int timeout = SeleniumExtensionMembers.defaultTimeout)
        {
            if (shouldCheckValueAfterSendKeys)
            {
                Driver.sendKeys(element, keys, shouldClickOnElementBeforeSendKeys);

                //wait for a specified time until element properties such as value is updated after sendKeys
                Thread.Sleep(500);
                
                //check whether element value equals expected value
                if (element.GetTextValue().Equals(keys))
                    return;
                else
                    //retry if element value and expected value do not match
                    throw new ArgumentException("Element value does not match with expected value");
            }
            else
                Driver.sendKeys(element, keys, shouldClickOnElementBeforeSendKeys);
        }
        private static void sendKeys(this IWebDriver Driver, IWebElement element, string keys, bool click)
        {
            //Move to element with no wait
            Driver.moveToElement(element);

            //Click if click flag is set to true
            if (click)
                //Click with no wait
                Driver.LeftClickAction(element);

            //Clear element and check whether it is cleared, retry if clear fails for a specified (short) time
            ThreadHelper.VoidWait(() =>
            {
                Driver.clear(element);
                if (!(element.GetTextValue().IsNullOrEmpty()))
                {
                    throw new ArgumentException("Element not yet cleared");
                }
            }, true, 500);

            //Send keys to the element   
            element.SendKeys(keys);

        }
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,364
Location
Chesapeake, VA
Programming Experience
10+
As I recall, SendKeys interprets the caret as a Ctrl key to be pressed along with the character that follows.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,364
Location
Chesapeake, VA
Programming Experience
10+
Yup, and the tilde is a special character as well. Please see the documentation:

 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,364
Location
Chesapeake, VA
Programming Experience
10+
So the part of the puzzle left is: does Selenium use the .NET Framework's SendKeys() or does it use something else. I'll leave that up to the OP to dig through Selenium's documentation to determine if it has its own special characters as well, or of just used the .NET Framework.
 
Top Bottom