Skip to content

Instantly share code, notes, and snippets.

@keys-github
Created March 22, 2024 11:00
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
// How to close Tab in Selenium Java Example
public class CloseTabBrowserShortcuts {
public static void main(String[] args) {
// No need for setProperty as ChromeDriver() will auto install
// required browser driver for Chrome using Selenium Manager
WebDriver driver = new ChromeDriver();
// Open the "https://ecommerce-playground.lambdatest.io/"
// web page on the browser
driver.get("https://ecommerce-playground.lambdatest.io/");
// Print the tile of the current tab
System.out.println(driver.getTitle());
// Add time Delay for page to load
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// Catch block to handle exception
e.printStackTrace();
}
// Selenium Close tab Java using browser shortcuts
Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys("w").keyUp(Keys.CONTROL).perform();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment