2

How to open a new tab in the same window session of the browser through Selenium WebDriver command?

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Bharadwaj Pendyala
  • 324
  • 1
  • 3
  • 16
  • 1
    Is 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 Answers5

6

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.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
1

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.

Helping Hands
  • 5,292
  • 9
  • 60
  • 127
1

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;
    }
Ankit Gupta
  • 776
  • 5
  • 12
0

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.

switch tabs using Selenium WebDriver with Java

Community
  • 1
  • 1
A user
  • 1,060
  • 3
  • 19
  • 47
0

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
Brydenr
  • 798
  • 1
  • 19
  • 30