Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

opening a new tab in node.js selenium-webdriver

I am trying to open a new tab using selenium-webdriver. But it opens the browser and stays there. It fails to open a new tab. What am I missing?

var webdriver = require('selenium-webdriver'),
  By = webdriver.By,
  until = webdriver.until;
  Keys = webdriver.Key;
  var driver = new webdriver.Builder()
   .forBrowser('firefox')
   .build();
  driver.get('https://google.com');
  driver.wait(until.titleIs('Google'), 5000);
  var selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,"t");  
  driver.findElement(By.css("body")).sendKeys(selectLinkOpeninNewTab);
  driver.quit();
like image 983
notnotundefined Avatar asked Feb 01 '17 07:02

notnotundefined


2 Answers

AFAIK webdriver.Key.chord doesn't send key combinations events, so it couldn't be used in this way.

To open new tab try to use JavaScript.

Try below and let me know if it doesn't work as expected:

driver.executeScript('window.open("newURL");');
like image 123
Andersson Avatar answered Sep 19 '22 08:09

Andersson


Note: This feature works with Selenium 4 and later versions.

// Opens a new tab and switches to new tab
await driver.switchTo().newWindow('tab');

// Opens a new window and switches to new window
await driver.switchTo().newWindow('window');

Source: https://www.selenium.dev/documentation/en/webdriver/browser_manipulation/

like image 44
JJunior Avatar answered Sep 20 '22 08:09

JJunior