Ashish Savaliya
Ashish Savaliya

Reputation: 85

How to open one by one link in new Tab from Menu using Selenium in Java

I am trying to open one by one link in new Tab in selenium Java, but only one link is opening the first time but For Loop goes wrong when open a second link, can anyone please help me to figure this out.

Here is my Code.

public class Link_Open_In_New_Tab {

    public WebDriver driver;

    @BeforeTest
    public void OpenBrowser() {

        System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.nopcommerce.com/");
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void OpenLink() throws InterruptedException {

        List<WebElement> ProMenu;
        WebElement SubLinks;
        driver.findElement(By.xpath("//ul[@class='top-menu']/li[1]/a")).click();
        ProMenu = driver.findElements(By.xpath("//ul[@class='top-menu']/li[1]/ul[@class='sublist']/li/a"));

        for (int i = 0; i < ProMenu.size(); i++) {

            SubLinks = driver
                    .findElement(By.xpath("//ul[@class='top-menu']/li[" + (i + 1) + "]/ul[@class='sublist']/li/a"));
            Actions act = new Actions(driver);
            act.keyDown(Keys.CONTROL).click(SubLinks).keyUp(Keys.CONTROL).build().perform();
            Thread.sleep(2000);

            String winHandleBefore = driver.getWindowHandle();

            for (String winHandle : driver.getWindowHandles()) {
                driver.switchTo().window(winHandle);
            }

            Thread.sleep(2000);

            driver.close();
            Thread.sleep(2000);

            driver.switchTo().window(winHandleBefore);
            Thread.sleep(2000);

            //driver.findElement(By.xpath("//ul[@class='top-menu']/li[1]/a")).click();
            //Thread.sleep(2000);
        }
    }

}

Upvotes: 1

Views: 1401

Answers (3)

Subburaj
Subburaj

Reputation: 2334

You were trying to open all the sublinks from the product menu. But your sublink xpath is pointing to the first sublink of all the menu (li[" + (i + 1) + "]/ul[@class='sublist']/li/a). So, you need to modify your sublink xpath as below and then try

SubLinks = driver.findElement(By.xpath("//ul[@class='top-menu']/li[1]/ul[@class='sublist']/li[" + (i + 1) + "]/a"));

Upvotes: 1

cruisepandey
cruisepandey

Reputation: 29362

You have to hover on Product, to get all the sub menus items. After then you can simulate keyboard strokes using Actions class which is available in Selenium and JAVA.

You can try this code :

public class Ashish  {

    static WebDriver driver ; 

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\user***\\Downloads\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.nopcommerce.com/");
        Actions action = new Actions(driver);
        action.moveToElement(driver.findElement(By.xpath("//ul[@class='top-menu']/li[1]/a"))).build().perform();  
        List<WebElement> element = driver.findElements(By.xpath("//ul[@class='top-menu']/li[1]/ul[@class='sublist']/li/a"));
        for(WebElement ele:element) {
            action.keyDown(Keys.LEFT_CONTROL).moveToElement(ele).click().keyUp(Keys.LEFT_CONTROL).build().perform();
        }


        }
}  

UPDATE 1 :

public class Ashish  {

    static WebDriver driver ; 

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\user***\\Downloads\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.nopcommerce.com/");
        WebDriverWait wait = new WebDriverWait(driver, 10);
        Actions action = new Actions(driver);
        action.moveToElement(driver.findElement(By.xpath("//ul[@class='top-menu']/li[1]/a"))).build().perform();  
        List<WebElement> element = driver.findElements(By.xpath("//ul[@class='top-menu']/li[1]/ul[@class='sublist']/li/a"));
        System.out.println(element.size());
        for(int i = 0 ; i<element.size() ; i++) {
            action.keyDown(Keys.LEFT_CONTROL).moveToElement(wait.until(ExpectedConditions.elementToBeClickable(element.get(i)))).click().keyUp(Keys.LEFT_CONTROL).build().perform();
            ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
            driver.switchTo().window(tabs.get(1));
            System.out.println(driver.getTitle());
            driver.close();
            driver.switchTo().window(tabs.get(0));
        }


        }
}

Console output :

9
nopCommerce - ASP.NET free shopping cart solution. What is nopCommerce?
nopCommerce - ASP.NET Open-source Ecommerce Shopping Cart Solution
nopCommerce - ASP.NET open source eCommerce solution. Feature list.
nopCommerce - Shopping Cart Demo & Shopping Cart Solution
nopCommerce - open source shopping cart. Showcase. Live Shops.
nopCommerce - open source shopping cart. Case Studies and Success Stories.
nopCommerce - ASP.NET open source shopping cart. Roadmap.
nopCommerce copyright removal key - nopCommerce
The nopCommerce Public License Version 3.0 ("NPL") - nopCommerce

Upvotes: 1

murali selenium
murali selenium

Reputation: 3927

If your intention is to test the links title to be working as expected or not then why need to you Crtl+click.

As per development, link here is to click but not ctrl+click, personally i am not willing to do this action.

Create variable say int i=1;

in loop like used, go for normal click and verify title. then increment i and go for browser back.

Upvotes: 0

Related Questions