Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium ChromeDriver C# - How to send a shortcut browser

How can I send a Chrome shortcut with Selenium ? I mean shortcuts like Ctrl+S, Ctrl+T or Ctrl+P which has nothing to do with WebElements. I read a lot of similar questions there, but none of the suggested solutions work for me.

Let's say I want to open a new tab (Ctrl+T) on the browser, I tried all the following code without success:

  1. The "standard" way :

    IWebElement body = myDriver.FindElement(By.TagName("body"));
    body.SendKeys(Keys.Control + "t");
    
  2. The action way :

    Actions action = new Actions(myDriver);
    action.SendKeys(Keys.Control + "t").Build().Perform();
    
  3. The ChromeDriver way 1 :

    if(myDriver is ChromeDriver)
    {
        ChromeDriver chromeDriver = myDriver as ChromeDriver;
        chromeDriver.Keyboard.SendKeys(Keys.Control + "t");
    }
    
  4. The ChromeDriver way 2 :

    ChromeDriver chromeDriver = myDriver as ChromeDriver;
    chromeDriver.Keyboard.PressKey(Keys.Control);
    chromeDriver.Keyboard.PressKey("t");
    chromeDriver.Keyboard.ReleaseKey(Keys.Control);
    chromeDriver.Keyboard.ReleaseKey("t");
    

Notice that the first way i mentionned worked for me with other WebDriver than Chrome. I use :

  • Selenium 3.0.1
  • ChromeDriver 2.27.440174

And my driver's initialization is really basic :

ChromeOptions options = new ChromeOptions();
this.myDriver = new ChromeDriver(/* my path */, options);

Any ideas?

like image 568
Florian K. Avatar asked Jan 27 '17 12:01

Florian K.


People also ask

What is Webdrivermanager ChromeDriver () setup ()?

chromedriver(). setup: checks for the latest version of the specified WebDriver binary. If the binaries are not present on the machine, then it will download the WebDriver binaries. Next, it instantiates the Selenium WebDriver instance with the ChromeDriver.

What is IWebDriver C#?

The IWebDriver interface is the main interface to use for testing, which represents an idealized web browser. The methods in this class fall into three categories: Control of the browser itself.


2 Answers

It seem to be Chromium issue. You cannot use keys combinations with chromedriver, but you still can use JavaScript as alternative:

IJavaScriptExecutor js = myDriver as IJavaScriptExecutor;
js.ExecuteScript("window.open()"); // Open new browser tab like `CTRL + t` do
like image 179
Andersson Avatar answered Nov 04 '22 10:11

Andersson


Unfortunately this issue currently prevents chrome from reacting to shortcuts like Ctrl+T sent by selenium.

like image 23
Der Tung Avatar answered Nov 04 '22 09:11

Der Tung