1

I know that I cannot use key combinations with chromedriver, but can use JavaScript as an alternative. The example below navigates to google.com and opens a new window tab.

IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://google.com");
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("window.open()"); // Open new browser tab like CTRL + t do

But can't figure out how to send these keyboard combinations...

CTRL + SHIFT + i
CTRL + SHIFT + m
GRF
  • 171
  • 2
  • 10

1 Answers1

1

Since you can run arbitrary JavaScript using .ExceuteScript you can use a JavaScript solution to send key presses to the Web page: Is it possible to simulate key press events programmatically?

This only works for shortcuts that the Web page itself handles. Since the examples for shotcuts you gave are actually browser shortcuts the JavaScript solution won't work.

Instead you have to send the keyboard presses to the Web browser directly. This is operating system dependent but under Windows you can use SendMessage to send keyboard presses to another process: Sending keyboard key to browser in C# using sendkey function

Edit: The method above makes it hard to send shortcuts with Shift/Control involved. When under Windows SendKeys (more info about the syntax) can be used to send keys to the current foreground window:

// reference System.Windows.Forms
SendKeys.SendWait ("^+{i}"); // CTRL + SHIFT + I
Thread.Sleep (500);
SendKeys.SendWait ("^+{m}"); // CTRL + SHIFT + M
a-ctor
  • 3,568
  • 27
  • 41
  • Thanks a-actor... So given the ChromeWrapper class shown in that post would this be the correct way to send the 2 key combos one right after the other ? **CTRL + SHIFT + i** and **CTRL + SHIFT + m** `ChromeWrapper chrome = new ChromeWrapper(@"https://stackoverflow.com"); Thread.Sleep(300); // CTRL + SHIFT + i chrome.SendKey((char)17); // ctrl chrome.SendKey((char)16); // shift chrome.SendKey((char)73); // I CTRL + SHIFT + m chrome.SendKey((char)17); // ctrl chrome.SendKey((char)16); // shift chrome.SendKey((char)77); // M ` – GRF Jun 07 '20 at 15:38
  • Also, how does this ChromeWrapper class relate to the Selenium Chrome Driver class? I want to send these 2 key combos to the Chrome Driver window. – GRF Jun 07 '20 at 15:50
  • @GRF I have updated my answer to include a simpler solution that can send keys to the current foreground window. – a-ctor Jun 07 '20 at 16:07
  • Thanks again. Works GREAT. Had to add s sleep of 10 seconds though so window would have time to react. – GRF Jun 07 '20 at 18:23