How can I send key chords to text area with Selenium?

前端 未结 5 1143
天涯浪人
天涯浪人 2021-01-02 03:42

I would like to simulate a user pressing shift-enter in a text area. Here is the code I am working with:

var driver = new FirefoxDriver();
driver.Navigate().         


        
相关标签:
5条回答
  • 2021-01-02 03:56

    for me, on c# only this variation works:

    actions.KeyDown(Keys.Control);
    actions.SendKeys("a");
    actions.KeyUp(Keys.Control);
    
    0 讨论(0)
  • 2021-01-02 03:57

    In Java, We have chord method, this method will send sequence of keys together:

    textArea.SendKeys( Keys.chord(Keys.Control , "a" ) );
    

    or

    textArea.SendKeys( Keys.chord ( Keys.Shift,Keys.Enter ) );
    
    0 讨论(0)
  • 2021-01-02 04:05

    Here's how keys.chord() is implemented in JavaScript: https://github.com/SeleniumHQ/selenium/blob/d8ddb4d83972df0f565ef65264bcb733e7a94584/javascript/node/selenium-webdriver/lib/input.js#L135

    which is already pretty close to the top accepted answer, I think the only difference is the Null character added to the end. You could probably make a helper method to help you remember it, but unfortunately OpenQA.Selenium.Keys is a static class and all of it's properties return strings. So if you wanted to do a nice wrapper around it you'd probably also have to wrap the keys class too.

    0 讨论(0)
  • 2021-01-02 04:11

    Simpler than I expected. Since SendKeys takes a string, and the static constants on Keys are all strings they can simply be concatenated together like this:

    textarea.SendKeys(Keys.Shift + Keys.Enter);
    
    0 讨论(0)
  • 2021-01-02 04:12

    If there's something that you might do over and over, it may be worth making an extension method for it. I did this since .Clear() doesn't work in our web app for some reason. Instead of always sending a CTRL+A and \b, I just extended it with this:

    public static class ExtensionMethods
    {
        public static void Blank(this IWebElement _el)
        {
            _el.SendKeys(Keys.Control + "a");
            _el.SendKeys("\b");
        }
    }
    

    Then I just call dynEl.Blank(); and it works great.

    0 讨论(0)
提交回复
热议问题