I tried to press Ctrl+T using Selenium (Java) in the Chrome browser. There is no error in the console, but the browser does not open a new tab. I want to know why this happens. I tried to find on Stack Overflow, but I didn't find any accurate answer.
This is my code:
package automation1;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class Action {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\asus\\OneDrive\\Desktop\\Jar\\chromedriver_win32\\chromedriver.exe");
WebDriver chromedriver = new ChromeDriver();
chromedriver.get("https://xenodochial-meninsky-118d47.netlify.app/");
chromedriver.manage().window().fullscreen();
Actions a = new Actions(chromedriver) ;
Thread.sleep(3000) ;
a.sendKeys(Keys.CONTROL +"t").build().perform() ;
}
}
Why doesn't this not open a new tab in the Chrome browser?
There is a solution without using org.openqa.selenium.interactions.Actions
.
For selenium 3.141.59:
pom.xml dependency:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
Code:
package tests;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class TejveerNaruka {
public static void main(String[] args) {
String chromedriverPath = System.getProperty("user.dir") + "\\resources\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", chromedriverPath);
ChromeOptions options = new ChromeOptions();
options.addArguments("--ignore-certificate-errors");
options.addArguments("--start-maximized");
options.addArguments("--disable-notifications");
WebDriver driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
driver.get("https://www.stackoverflow.com");
System.out.println(driver.getTitle());
// open new tab using javascript
jsExecutor.executeScript("window.open()");
// get Window handles, identifications of all browser tabs and windows
List<String> windowHandles = new ArrayList<String> (driver.getWindowHandles());
// switch to second tab
driver.switchTo().window(windowHandles.get(1));
// prints title of the second tab (empty line)
System.out.println(driver.getTitle());
// navigate driver in the second tab
driver.get("https://www.zemancountdown.cz/");
// prints title of the second tab
System.out.println(driver.getTitle());
// close second tab
jsExecutor.executeScript("window.close()");
// switch to the first tab
driver.switchTo().window(windowHandles.get(0));
//prints title of the first tab
System.out.println(driver.getTitle());
driver.quit();
}
}
Output:
Starting ChromeDriver 108.0.5359.71 (1e0e3868ee06e91ad636a874420e3ca3ae3756ac-refs/branch-heads/5359@{#1016}) on port 18498
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Pro 12, 2022 12:50:43 ODP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Stack Overflow - Where Developers Learn, Share, & Build Careers
Miloš Zeman Countdown
Stack Overflow - Where Developers Learn, Share, & Build Careers
Another example now with selenium 4:
pom.xml dependency:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.7.1</version>
</dependency>
Code:
package tests;
import java.time.Duration;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class TejveerNaruka {
public static void main(String[] args) {
String chromedriverPath = System.getProperty("user.dir") + "\\resources\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", chromedriverPath);
ChromeOptions options = new ChromeOptions();
options.addArguments("--ignore-certificate-errors");
options.addArguments("--start-maximized");
options.addArguments("--disable-notifications");
WebDriver driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
// navigate driver and print title
driver.get("https://www.stackoverflow.com");
System.out.println(driver.getTitle());
// create second driver in new tab
WebDriver driver2 = driver.switchTo().newWindow(WindowType.TAB);
// prints empty line
System.out.println(driver2.getTitle());
// navigate driver2 and print title
driver2.get("https://www.zemancountdown.cz/");
System.out.println(driver2.getTitle());
driver2.quit();
driver.quit();
}
}
Output:
Starting ChromeDriver 108.0.5359.71 (1e0e3868ee06e91ad636a874420e3ca3ae3756ac-refs/branch-heads/5359@{#1016}) on port 24322
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Stack Overflow - Where Developers Learn, Share, & Build Careers
Miloš Zeman Countdown