Pressing Ctrl+A in Selenium WebDriver

前端 未结 13 877
一向
一向 2020-11-27 02:45

I need to press Ctrl+A keys using Selenium WebDriver. Is there any way to do it?

I checked the Selenium libraries and found that Selenium allow

相关标签:
13条回答
  • 2020-11-27 03:31

    It works for me:

    OpenQA.Selenium.Interactions.Actions action 
        = new OpenQA.Selenium.Interactions.Actions(browser);
    action.KeyDown(OpenQA.Selenium.Keys.Control)
        .SendKeys("a").KeyUp(OpenQA.Selenium.Keys.Control).Perform();
    
    0 讨论(0)
  • 2020-11-27 03:31

    This is what worked for me using C# (VS2015) with Selenium:

    new Actions(driver).SendKeys(Keys.Control+"A").Perform();
    

    You can add as many keys as wanted using (+) inbetween.

    0 讨论(0)
  • 2020-11-27 03:33

    Simplest answer in C# (if you are C# inclined).

    Actions action = new Actions(); 
    action.KeyDown(OpenQA.Selenium.Keys.Control).SendKeys("a").KeyUp(OpenQA.Selenium.Keys.Control).perform();
    

    This answer is almost given above by Hari Reddy but I have fixed the case which he'd got wrong on some keywords, added the KeyUp or you get in a mess leaving the control key down, I've also added the clarification on OpenQA.Selenium.Keys because you may also be using Windows.Forms on the same class as I was an require this clarity. Lastly, I type "a" because I found that to be the simplest way and I can see no suggestion from the OP that they don't want the simplest answer.

    Many thanks to Hari Reddy though as I was a novice in Actions class usage and I was writing many different commands, chaining them together the way he showed is quicker :-)

    0 讨论(0)
  • 2020-11-27 03:35

    In Selenium for C#, sending Keys.Control simply toggles the Control key's state: if it's up, then it becomes down; if it's down, then it becomes up. So to simulate pressing Control+A, send Keys.Control twice, once before sending "a" and then after.

    For example, if we is an input IWebElement, the following statement will select all of its contents:

    we.SendKeys(Keys.Control + "a" + Keys.Control);

    0 讨论(0)
  • 2020-11-27 03:39

    To click Ctrl+A, you can do it with Actions

      Actions action = new Actions(); 
      action.keyDown(Keys.CONTROL).sendKeys(String.valueOf('\u0061')).perform();
    

    \u0061 represents the character 'a'

    \u0041 represents the character 'A'

    To press other characters refer the unicode character table - http://unicode.org/charts/PDF/U0000.pdf

    0 讨论(0)
  • 2020-11-27 03:39

    Since Ctrl+A maps to ASCII code value 1 (Ctrl+B to 2, up to, Ctrl+Z to 26).

    Try:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using OpenQA.Selenium;
    using OpenQA.Selenium.IE;
    using OpenQA.Selenium.Support.UI;
    using OpenQA.Selenium.Interactions;
    using OpenQA.Selenium.Internal;
    using OpenQA.Selenium.Remote;
    
    namespace SeleniumHqTest
    {
        class Test
        {
                IWebDriver driver = new InternetExplorerDriver();
                driver.Navigate().GoToUrl("http://localhost");
                IWebElement el = driver.FindElement(By.Id("an_element_id"));
                char c = '\u0001'; // ASCII code 1 for Ctrl-A
                el.SendKeys(Convert.ToString(c));
                driver.Quit();
        }
    }
    
    0 讨论(0)
提交回复
热议问题