How to open a new tab in the same window session of the browser through Selenium WebDriver command?
- 36,924
 - 42
 - 155
 - 176
 
- 324
 - 1
 - 3
 - 16
 
- 
                    1Is there any specific reason you want to open a new tab in the same browser window? Because, I am afraid you won't be able to perform any further actions on it, as [selenium can't switch between tabs. It can only switch between windows.](http://stackoverflow.com/a/11358741/4193730). – Subh Jan 27 '15 at 07:25
 - 
                    look here: https://stackoverflow.com/questions/17547473/how-to-open-a-new-tab-using-selenium-webdriver-with-java – AcMuD Apr 19 '18 at 07:47
 
5 Answers
Opening a new tab in the same browser window is possible, see solutions for Firefox:
The problem is - once you've opened a tab, there is no built-in easy way to switch between tabs. Selenium simply doesn't provide an API for that.
Instead of a tab, open a new browser window.
Yes you can do that , See below my sample code for that :
   //OPEN SPECIFIC URL IN BROWSER
    driver.get("http://www.toolsqa.com/automation-practice-form/");
   //MAXIMIZE BROWSER WINDWO
    driver.manage().window().maximize();
   //OPEN LINKED URL IN NEW TAB IN SAME BROWSER 
   String link1 = Keys.chord(Keys.CONTROL,Keys.ENTER); 
   driver.findElement(By.linkText("Partial Link Test")).sendKeys(link1);
Above code will open link1 in new tab. you can run above code to see effect. Above is public link includes testing form.
But as @alecxe told that there is no way to switch between tabs. So better you open new browser window.
- 5,292
 - 9
 - 60
 - 127
 
Using java script we can easily open new tab in the same window.
public String openNewTab(){
       String parentHandle = driverObj.getWindowHandle();
        ((JavascriptExecutor)driverObj).executeScript("window.open()");
        String currentHandle ="";
                // below driver is your webdriver object
        Set<String> win  = driver.getWindowHandles();   
        Iterator<String> it =  win.iterator();
        if(win.size() > 1){
            while(it.hasNext()){
                String handle = it.next();
                if (!handle.equalsIgnoreCase(parentHandle)){
                    driver.switchTo().window(handle);
                    currentHandle = handle;
                }
            }
        }
        else{
            System.out.println("Unable to switch");
        }
        return currentHandle;
    }
- 776
 - 5
 - 12
 
I am afraid, but there is a way to switch between tab's . We have successful answers for this problem.
Please find the below link.
Selenium can switch between tabs.
Python:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
element = driver.find_element_by_css_selector("html") # Gets the full page element, any element works for this
key_code = Keys.CONTROL # Use Keys.COMMAND for Mac
driver.execute_script("window.open();") # Executes Javascript to open a new tab
driver.switch_to.window(1) # Switches to the new tab (at index 1, first tab is index 0)
driver.switch_to.window(0) # Switches to the first tab
- 798
 - 1
 - 19
 - 30