John John - 1 year ago 47
Java Question

Selenium throws IndexOutOfBoundsException while opening new tab

Selenium Java program throws IndexOutOfBoundsException exception when executed below code to open new tab in Chrome.

Java Code:

System.setProperty("", "/Users/john/Desktop/chromedriver");
WebDriver driver = new ChromeDriver();
String baseUrl = "http://google";
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");

ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1)); // switches to new tab


Starting ChromeDriver 2.29.461585 (0be2cd95f834e9ee7c46bcc7cf405b483f5ae83b) on port 48571
Only local connections are allowed.
May 12, 2017 12:54:14 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(
at java.util.ArrayList.get(
at test.SeleniumTest.main(

Answer Source

The size of tabs is 1. You try to access position 1, while the highest possible is 0.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download