Click here to Skip to main content
15,908,274 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how to open a new tab in a new window that is opened in selenium firefox webdriver?

What I have tried:

the following code:


public static void main(String[] args) {

System.setProperty("webdriver.firefox.marionette", "some directory"); 
    		  
    		   WebDriver driver = new FirefoxDriver();
               driver.get("some url");
               driver.get(By.cssSelector("some button with link")).click();
    		   
    		   driver.close();
    		   
    		   for(String winHandle : driver.getWindowHandles()){
    			    driver.switchTo().window(winHandle);
    			}

              driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");

//everything works fine til here

ArrayList<String> tabs = new ArrayList<String (driver.getWindowHandles());
            	    
            	    driver.switchTo().window(tabs.get(1));
            	    
            	    driver.get("another url");



throws
exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1



where could be the problem
Posted
Updated 1-Apr-22 3:07am

1 solution

Java
driver.switchTo().window(tabs.get(1));

The error message is clearly telling you that you only have one tab in existence, so index 1 is invalid (indexes start at 0). And it is always a bad idea to hard code values for dynamic arrays. The first thing you should do is to check how many tabs are in existence.
 
Share this answer
 
Comments
T1xT 1-Apr-22 11:31am    
yes. actually selenium does not consider the main window as a tab; so there is only one tab over there which of course has the index 0.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900