简体   繁体   中英

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("webdriver.chrome.driver", "/Users/john/Desktop/chromedriver");
                    WebDriver driver = new ChromeDriver();
                    driver.manage().window().maximize();
                    String baseUrl = "http://google";
                    driver.get(baseUrl);
                    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
                    driver.get("https://www.facebook.com");

Error:

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(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at test.SeleniumTest.main(SeleniumTest.java:31)

In the line driver.switchTo().window(tabs.get(1)); you get that exception. It's happening because, you've no any other windows open other than the first one.

tabs的大小为 1。您尝试访问位置1 ,而最高可能是0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM