微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Selenium C# - Sendkeys 方法未输入 ^~ 字符

如何解决Selenium C# - Sendkeys 方法未输入 ^~ 字符

我想在输入框中传递一个文本,但它没有输入特殊字符并添加空格而不是 - 有人可以帮助我吗?

我想在输入框中输入的文本:"This is a ^~ in C#.";

我尝试了以下内容

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

但它输入以下文本:

这是 C# 中的一个

发送密钥代码

    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,int timeout = SeleniumExtensionMembers.defaultTimeout)
    {
        if (shouldCheckValueAfterSendKeys)
        {
            Driver.sendKeys(element,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,shouldClickOnElementBeforeSendKeys);
    }
    private static void sendKeys(this IWebDriver Driver,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");
            }
        },500);

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

    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。