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