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();
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");');
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With