4

I am trying to create a test script in Node.JS using selenium webdriver and in case I need to send keyboard shortcuts to the driver.

I tried finding the //body element and sending them but only ctrl+a works there. Is there any alternative to that?

I want to simulate ctrl + tab and ctrl + t

  • @FrederikDeMets The suggested question is not in nodejs, also tried it did not work, I do not want to perform something on an element (as the question was asked) but open tabs and navigate to them etc. Browser specific stuff and not website specific. And as I said in my question. I am ok with ctrl+a. The question you suggest wants to perform ctrl+a – John Demetriou Mar 26 '15 at 9:52
  • Sorry, I misread your question. I'm not sure if that's possible with Selenium though, considering the effort it takes to switch focus to other windows I'd be surprised if the much easier shortcuts would work. – FDM Mar 26 '15 at 9:57
  • This is an alternative solution: stackoverflow.com/questions/14376413/… – FDM Mar 26 '15 at 9:59
  • Again this is not for nodejs/javascript. But I tested something similar and finding html using tagname or xpath does not really work in javascript – John Demetriou Mar 26 '15 at 16:03
4

WebDriver cannot be used todo browser specific actions, it can only be used to drive the web-page. Although sending the keys CTRL-TAB might switch tabs WebDriver will still think you are in the previous window.

Switching Tabs as you want is thus actually something different. Each tab is actually a window organised in a tab like look-and-feel. Thus you have to use driver.switchTo().window('windowName'); to switch "tabs" and to find the window names use driver.getAllWindowHandles() which returns a list of windows.

For sending key combinations with the JavaScript version of WebDriver have a look at ActionSequence. It has a small example and here is a list of possible keys.

Would look something like this:

new webdriver.ActionSequence(driver).
keyDown(webdriver.Key.CONTROL).
keyDown(webdriver.Key.TAB)
keyUp(webdriver.Key.CONTROL).
keyUp(webdriver.Key.TAB).
perform();
driver.swithTo().window('name');

This thread also shows the difference between WebDriverJS and WebDriver.IO including examples how-to use the keyDown.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.