2

I am running Selenium Webdriver to test a web application that is under development (I have created quite a few tests so far). I have been trying to open a few new tabs in the window opened by Selenium but no success so far. I looked through quite a bit of different solutions but most of them for Java or Python and I'm using Javascript (I need to use Javascript).

Selenium Webdriver: v.3.1.0 OS: Xubuntu 16.04 Browsers: Chrome 55.0.2883.87 and Firefox 50.1.0

I have tried various solutions including:

  • action sequences, which do nothing in both Chrome and Firefox but complains in Firefox:

    driver.actions().keyDown(Key.CONTROL).sendKeys('n').keyUp(Key.CONTROL).perform();

  • using Key.chord(), which results in no errors, no reaction but it does send the keys - Firefox giving a strange charCode after pressing the buttons

    driver.findElement(By.css("body")).sendKeys(Key.chord(Key.CONTROL, 't'));

  • Key.CONTROL only, which also results in no errors, no reaction but it does send the keys - Firefox giving a strange charCode after pressing the buttons

    driver.findElement(By.css("body")).sendKeys(Key.CONTROL + "t");

What I do at the moment is to navigate the driver to a website with javascript keypress detection and see if they were clicked after the 'aaa' :

driver.get("http://unixpapa.com/js/testkey.html");
driver.findElement(By.css("body")).sendKeys("aaa");
driver.findElement(By.css("body")).sendKeys(Key.CONTROL + "t");

This navigates to the page and it it oputputs that on the page detection area:

keydown  keyCode=17        which=17        charCode=0        
keydown  keyCode=84  (T)   which=84  (T)   charCode=0        
keypress keyCode=116 (t)   which=116 (t)   charCode=116 (t)  
keyup    keyCode=84  (T)   which=84  (T)   charCode=0        
keyup    keyCode=17        which=17        charCode=0  

which I believe means that they have been clicked. However, there is no reaction and no tabs created. No error displayed anywhere, no complains. Nothing. I was not sure if this is a bug or a problem or something that I might not be doing right. So if anyone has any idea, please help.

0

3 Answers 3

2

To open new tab you can try to use

driver.executeScript('window.open();');
Sign up to request clarification or add additional context in comments.

5 Comments

Hi Andersson, Thank you for your answer. I believe this will open a new window right? And I will need to switch between window handles and this is my safety net. However, I want to know if I can open new tabs not windows.
AFAIK this depends on browser you use and personal settings. Try it to check whether it opens window or tab in your case. Also note that even if new tab opened, you still need to switch to it using driver.switch_to.window(driver.window_handles[-1])...
Oh wow it does open a new tab. I have been avoiding this since I though it was only for opening a window and not a tab. Fantastic. Thanks a lot, saved my day :) . Yeah, having to switch was not the problem but I did want it to open a new tab which this does. Thanks again
Welcome. Also note that if you want to open URL in new tab you can just pass it as argument to open() as window.open('https://google.com')
Fantastic! Yeah, having to switch was not the problem but I did want it to open a new tab which this does. Thanks again!
0

Try using following (Robot Class):

   Robot robo = new Robot();
   robo.keyPress(KeyEvent.VK_CONTROL);
   robo.keyPress(KeyEvent.VK_T);
   robo.keyRelease(KeyEvent.VK_CONTROL);
   robo.keyRelease(KeyEvent.VK_T);

1 Comment

Hi Kushal. Thank you for your answer. I am using pure Selenium and I believe this is robot framework, right? (Not sure since I have not used it before and fairly new to Selenium) Also not sure that this is javascript. Since I have quite a few tests running so far in javascript using pure Selenium javascript binding it is important for me to keep on using them.
0
protected void openNewTab(String url) {
        ((JavascriptExecutor) driver).executeScript("window.open('" + url + "','_blank');");
    }

use this method to open a new tab with a url

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.