'Unable to open a link in new tab using Actions Class

I am trying to open a link in a new tab using selenium java automation code.

Initially i tried with actions class but it wasn't working. Later i tried using the Keys to automate the same thru keyboard actions and it worked. But i wanted to know why am i unable achieve the same with Actions class. Just wanted to if i am doing anything wrong here or is Actions class not suitable for this.

Below is the code snippet i have written.

public class InterviewQuestion {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub

        //System.out.println(System.getProperty("user.dir")+"\\"+"chromedriver.exe");
        
        System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\"+"chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.google.co.in/");
        driver.manage().window().maximize();
        WebElement GoogleSearchTextBox = driver.findElement(By.xpath("//input[@title='Search']"));
        GoogleSearchTextBox.sendKeys("test automation");
        GoogleSearchTextBox.sendKeys(Keys.ENTER);
        **boolean useActionsClass = false,useKeys = true;**

        // finding the required element to be clicked
        WebElement RequiredSearchResult = driver.findElement(By.xpath("//div[@class='EIaa9b']/div[1]/div[2]/a"));
        
        if(**useActionsClass**)
        {
            Actions actions = new Actions(driver);
            //actions.moveToElement(RequiredSearchResult).build().perform();
            Thread.sleep(5000);
            actions.moveToElement(RequiredSearchResult).contextClick(RequiredSearchResult).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
        }
        if(useKeys)
        {
            Thread.sleep(5000);
            String s = Keys.chord(Keys.CONTROL,Keys.ENTER);
            RequiredSearchResult.sendKeys(s);
            Set<String> windows = driver.getWindowHandles();
            Iterator itr = windows.iterator();
            Object FirstWindowHandle = itr.next();
            Object SecondWindowHandle = itr.next();
            driver.switchTo().window((String) SecondWindowHandle);
            Thread.sleep(5000);
            //driver.switchTo().window((String) FirstWindowHandle);
        }

        //driver.quit();
    }
}


Solution 1:[1]

First, get the link with the help of href attribute from your RequiredSearchResultLink element. once you have the link, use selenium 4 driver.switchTo().newWindow(WindowType.TAB); method to open a new tab. Once the tab is open, just pass your get method, the url which you got from your link field.

If you are not using selenium 4, I will strongly suggest using it, as it will remove lots of unnecessary code from the action class.

Selenium 4 dependency:

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0</version>
</dependency>

Code with Selenium 4:

driver.get("https://www.google.co.in/");
    driver.manage().window().maximize();
    WebElement GoogleSearchTextBox = driver.findElement(By.xpath("//input[@title='Search']"));
    GoogleSearchTextBox.sendKeys("test automation");
    GoogleSearchTextBox.sendKeys(Keys.ENTER);
    WebElement RequiredSearchResultLink = driver.findElement(By.xpath("//div[@class='EIaa9b']/div[1]/div[2]/a"));
    String href = RequiredSearchResultLink.getAttribute("href");
    driver.switchTo().newWindow(WindowType.TAB);
    driver.get(href);
    Thread.sleep(10);
    driver.quit();
}

Solution 2:[2]

3 things you need to

  1. Copy the link you need to open in new tab

  2. Open new tab

  3. Paste your link

    driver.switchTo().newWindow(WindowType.TAB);

    driver.get("your new lin");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Jayanth Bala