0

I want to open multiple url on the browser with the following command

public static void main(String[] args) {
    System.setProperty("webdriver.chrome.driver", "E:\\Java\\Library\\seleniumhq\\chromedriver_win32_2.40.exe");
    WebDriver webDriver = new ChromeDriver();

    openNewTab(webDriver, "http://dantri.com.vn/phap-luat.htm", 1);
    openNewTab(webDriver, "http://dantri.com.vn/xa-hoi.htm", 2);
    openNewTab(webDriver, "http://dantri.com.vn/the-gioi.htm", 3);
    openNewTab(webDriver, "http://dantri.com.vn/the-thao.htm", 4);
    openNewTab(webDriver, "http://dantri.com.vn/giao-duc-khuyen-hoc.htm", 5);

}

public void openNewTab(WebDriver webDriver, String url, int position) {
    ((JavascriptExecutor) webDriver).executeScript("window.open()");

    ArrayList<String>  tabs = new ArrayList<>(webDriver.getWindowHandles());
    System.out.println("tabs : " + tabs.size() + " >position: " + position + " >\t" + url);
    webDriver.switchTo().window(tabs.get(position));

    webDriver.get(url);
}

Although the browser tab has been opened, tabs.size () remains the same, resulting in an error.

tabs : 2 >position: 1 > http://dantri.com.vn/phap-luat.htm
tabs : 2 >position: 2 > http://dantri.com.vn/xa-hoi.htm
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
    at java.util.ArrayList.rangeCheck(ArrayList.java:657)
    at java.util.ArrayList.get(ArrayList.java:433)
    at test.test.NewClass.openNewTab(NewClass.java:37)
    at test.test.NewClass.<init>(NewClass.java:25)
    at test.test.NewClass.main(NewClass.java:43)

Please help me fix it

7
  • Please add 'String' parameter to ArrayList. Here tabs = new ArrayList<String>(webDriver.getWindowHandles()); Commented Jun 21, 2018 at 5:13
  • Tested by you, but it's still buggy Commented Jun 21, 2018 at 5:19
  • Please also check by removing Static keyword from this line "static ArrayList<String> tabs" Commented Jun 21, 2018 at 5:22
  • still does not work Commented Jun 21, 2018 at 5:30
  • Are you getting same exeception ? Check with debugger, Where exactly you are getting null values/responce. Commented Jun 21, 2018 at 5:32

1 Answer 1

2

Change your openNewTab method as static method . I am not getting the exception and getting the response as expected.

Modified Code:

public class MultiTab {

    public static void main(String args[]){
        System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");
        WebDriver webDriver =new ChromeDriver();
        openNewTab(webDriver, "http://dantri.com.vn/phap-luat.htm", 1);
        openNewTab(webDriver, "http://dantri.com.vn/xa-hoi.htm", 2);
        openNewTab(webDriver, "http://dantri.com.vn/the-gioi.htm", 3);
        openNewTab(webDriver, "http://dantri.com.vn/the-thao.htm", 4);
        openNewTab(webDriver, "http://dantri.com.vn/giao-duc-khuyen-hoc.htm", 5);
    }

    public static void openNewTab(WebDriver webDriver, String url, int position) {
        ((JavascriptExecutor) webDriver).executeScript("window.open()");
        ArrayList<String> tabs = new ArrayList<>(webDriver.getWindowHandles());
        System.out.println("tabs : " + tabs.size() + " >position: " + position + " >\t" + url);
        webDriver.switchTo().window(tabs.get(position));
        webDriver.get(url);
    }
}

Output:

tabs : 2 >position: 1 > http://dantri.com.vn/phap-luat.htm
tabs : 3 >position: 2 > http://dantri.com.vn/xa-hoi.htm
tabs : 4 >position: 3 > http://dantri.com.vn/the-gioi.htm
tabs : 5 >position: 4 > http://dantri.com.vn/the-thao.htm
tabs : 6 >position: 5 > http://dantri.com.vn/giao-duc-khuyen-hoc.htm
Sign up to request clarification or add additional context in comments.

4 Comments

I still faulty, can you please give me the selemium version and chromedriver you use it
I am using Selenium Version: 3.11.0
I checked the command it does not work properly, may be wrong when the tabs = new ArrayList<>(webDriver.getWindowHandles()); are not updated value.I want the statement to be more stable than to continue helping me
if pass two urls its opening 3 tabs... i want to open only 2 tabs for 2 urls

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.