-->

Sending '@' special character with SendKey

2019-05-21 11:14发布

问题:

I'm trying to send a special character to a textbox using the SendKeys method in Internet Explorer 11. I didn't have this problem while trying to send special characters in other browsers, but Internet Explorer is sending completely different characters when trying to send special characters.

Most of the specials characters do work (e.g. !#()$%&/) but the characters like @{}§ are not working, and instead of those characters, something like vbnm is written in the textbox. These letters represents the keys with which the special signs are created in combination with the key Alt Gr (e.g. Alt Gr + v = @, Alt Gr + b = {, Alt Gr + n = }, Alt Gr + m = §).

I tried using the Action object to send the keys with KeyDown method, but it was also unsuccessful, when trying to write a string with '@' in it (e.g. username@mail.com) the special sign is replaced with the letter 'v' (usernamevmail.com). I also tried to use the ASCII equivalent of the '@' sign but instead of the special character, I got the '64' text placed in the textbox.

private static string UserName
    {
        get { return Browser.GetSingleElement(Login.txtUserName).GetAttribute("value"); }
        set
        {
            if (value == null) return;
            var domElement = Browser.GetSingleElement(Login.txtUserName);
            Actions action = new Actions(Browser._driver);
            action.MoveToElement(domElement);
            action.Perform();
            domElement.Clear();
            action.KeyDown(Keys.LeftAlt);
            action.Perform();
            action.SendKeys(Keys.NumberPad6);
            action.SendKeys(Keys.NumberPad4);
            action.Perform();
            //action.MoveToElement(Browser.GetSingleElement(Login.txtUserName)).Click().KeyDown(Keys.LeftAlt).SendKeys("64").Perform();
            if (!Browser.SendKeys(Browser.GetSingleElement(Login.txtUserName), "@{}§~$%&()="))
                throw new Exception("Failed to send keys on Login.txtUserName DOM element.");
        }
    }

When sending "@{}§~$%&()=" string to the textbox I get this as an output.

回答1:

string a = "@{}§~$%&()=";
string b = WebUtility.HtmlDecode(a);