How to open a link in a new tab in a menu using Selenium in Java


Ashish Savaliya

I am trying to open a link in a new Tab in Selenium Java, but the first time only one link opens, but when opening the second link the For Loop is giving an error, can anyone help me with this.

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);
        }
    }

}

suburbs

You tried to open all sublinks in the product menu. But your sublink xpath points to the first sublink of all menus ( li[" + (i + 1) + "]/ul[@class='sublist']/li/a) . So you need to modify the sublink xpath as below and try

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

Related


How to open a link in a new tab in a menu using Selenium in Java

Ashish Savaliya I am trying to open a link in a new Tab in Selenium Java, but the first time only one link opens, but when opening the second link the For Loop is giving an error, can anyone help me with this. Here is my code. public class Link_Open_In_New_Tab

How to open link in new tab using python and selenium

User 3820991 I want to open links found on a website in a new tab. I tried to open a new tab and passed the link URL to the motorists suggested here however, the new tab does not open at all. (There are some other suggestions on how to open new tabs, but none

How to open link in new tab using python and selenium

User 3820991 I want to open links found on a website in a new tab. I tried to open a new tab and passed the link URL to the motorists suggested here however, the new tab does not open at all. (There are some other suggestions on how to open new tabs, but none

How to open a link in a new tab using JavaScript

ash I am working on a website where I have to open a URL from the backend. I am using c# now. My problem is that I want to open the link in a new tab instead of a new window. my code is here: string url = ppHref.ToString(); string newScript = "<script languag

How to open a link in a new tab using JavaScript

ash I am working on a website where I have to open a URL from the backend. I am using c# now. My problem is that I want to open the link in a new tab instead of a new window. my code is here: string url = ppHref.ToString(); string newScript = "<script languag

How to open new tab using selenium python?

Good face I'm trying to make a program that can open multiple websites, but I can't press control-t. I tried multiple solutions but couldn't find one that worked. When I execute the keydown method, I get an error message webdriver has no attribute key_down Wh

Java Selenium: open new tab

Danny Cobb So I URLwant to browse this link in a new tab, which is not clickable, so when I click this button, nothing happens and will not work (even if not manually): WebElement hrefLink; actions.keyDown(Keys.SHIFT).click(hrefLink).keyUp(Keys.SHI

Java Selenium: open new tab

Danny Cobb So I URLwant to browse this link in a new tab, which is not clickable, so when I click this button, nothing happens and will not work (even if not manually): WebElement hrefLink; actions.keyDown(Keys.SHIFT).click(hrefLink).keyUp(Keys.SHI

Selenium - click on the link to open a new tab

tree 55 I've seen a lot of threads on how to open a link in a new tab, but what if you have a link that creates a new tab and you need to verify the title? All I need to do is click the link -> confirm the new tab has the correct title -> close the tab and con

Selenium - click on the link to open a new tab

tree 55 I've seen a lot of threads on how to open a link in a new tab, but what if you have a link that creates a new tab and you need to verify the title? All I need to do is click the link -> confirm the new tab has the correct title -> close the tab and con

Selenium - click on the link to open a new tab

tree 55 I've seen a lot of threads on how to open a link in a new tab, but what if you have a link that creates a new tab and you need to verify the title? All I need to do is click the link -> confirm the new tab has the correct title -> close the tab and con

Selenium - click on the link to open a new tab

tree 55 I've seen a lot of threads on how to open a link in a new tab, but what if you have a link that creates a new tab and need to validate the title? All I need to do is click the link -> confirm the new tab has the correct title -> close the tab and conti

How to open a link in a new tab/window using CSS?

Trey Shaffer Is it possible to style a hyperlink label in CSS in such a way that when the link is clicked, it will open it in a new tab or window? I know of solutions in HTML and JavaScript/jQuery, but is it possible to specify this property using a stylesheet

How to open a link in a new tab/window using CSS?

Trey Shaffer Is it possible to style a hyperlink label in CSS in such a way that when the link is clicked, it will open it in a new tab or window? I know of solutions in HTML and JavaScript/jQuery, but is it possible to specify this property using a stylesheet

How to open link in new tab when using css button

Nina Norshard Here is my current code: <a href="https://www.google.com" target="_blank" style="font-size:150%;color:#fa5252;">GOOGLE</a><br><br> I wanted it to be a button, so I replaced mine with: <button onclick="window.open='https://www.google.com'" class=