package com.
java;
import java.util.Set;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Main {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "./chromedriver");
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless"); // Run Chrome in headless mode
options.addArguments("--no-sandbox");
options.addArguments("--disable-dev-shm-usage");
// Initialize WebDriver
WebDriver driver = new ChromeDriver(options);
// Step 1: Navigate to Wikipedia
driver.get("https://www.wikipedia.org/");
// Step 2: Locate the link to the Play Store at the bottom of the page
WebElement playStoreLink =
driver.findElement(By.xpath("//a[contains(@href, 'play.google.com')]"));
// Step 3: Click on the Play Store link to open a new tab
playStoreLink.click();
// Step 4: Get the main window handle
String mainWindowHandle = driver.getWindowHandle();
// Step 5: Get all window handles
Set<String> allWindowHandles = driver.getWindowHandles();
// Step 6: Switch to the new window
for (String handle : allWindowHandles) {
if (!handle.equals(mainWindowHandle)) {
driver.switchTo().window(handle);
break;
}
}
// Step 7: Print the title of the new tab
System.out.println("New Tab Title: " + driver.getTitle());
// Step 8: Switch back to the main window
driver.switchTo().window(mainWindowHandle);
}
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.java</groupId>
<artifactId>SeleniumWithJava</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.53.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-htmlunit-driver</artifactId>
<version>2.52.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Maven Compiler Plugin for Java 1.8 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>