Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7308437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:33:26+00:00 2026-05-28T23:33:26+00:00

I have trawled the web and the WebDriver API. I don’t see a way

  • 0

I have trawled the web and the WebDriver API. I don’t see a way to open new tabs using WebDriver/Selenium2.0 .

Can someone please confirm if I am right?

Thanks,
Chris.
P.S: The current alternative I see is to either load different urls in the same window or open new windows.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T23:33:27+00:00Added an answer on May 28, 2026 at 11:33 pm

    There is totally a cross-browser way to do this using webdriver, those who say you can not are just too lazy. First, you need to use WebDriver to inject and anchor tag into the page that opens the tab you want. Here’s how I do it (note: driver is a WebDriver instance):

    /**
     * Executes a script on an element
     * @note Really should only be used when the web driver is sucking at exposing
     * functionality natively
     * @param script The script to execute
     * @param element The target of the script, referenced as arguments[0]
     */
    public void trigger(String script, WebElement element) {
        ((JavascriptExecutor)driver).executeScript(script, element);
    }
    
    /** Executes a script
     * @note Really should only be used when the web driver is sucking at exposing
     * functionality natively
     * @param script The script to execute
     */
    public Object trigger(String script) {
        return ((JavascriptExecutor)driver).executeScript(script);
    }
    
    /**
     * Opens a new tab for the given URL
     * @param url The URL to 
     * @throws JavaScriptException If unable to open tab
     */
    public void openTab(String url) {
        String script = "var d=document,a=d.createElement('a');a.target='_blank';a.href='%s';a.innerHTML='.';d.body.appendChild(a);return a";
        Object element = trigger(String.format(script, url));
        if (element instanceof WebElement) {
            WebElement anchor = (WebElement) element; anchor.click();
            trigger("var a=arguments[0];a.parentNode.removeChild(a);", anchor);
        } else {
            throw new JavaScriptException(element, "Unable to open tab", 1);
        }       
    }
    

    Next, you need to tell webdriver to switch its current window handle to the new tab. Here’s how I do that:

    /**
     * Switches to the non-current window
     */
    public void switchWindow() throws NoSuchWindowException, NoSuchWindowException {
        Set<String> handles = driver.getWindowHandles();
        String current = driver.getWindowHandle();
        handles.remove(current);
        String newTab = handles.iterator().next();
        locator.window(newTab);
    }
    

    After this is done, you may then interact with elements in the new page context using the same WebDriver instance. Once you are done with that tab, you can always return back to the default window context by using a similar mechanism to the switchWindow function above. I’ll leave that as an exercise for you to figure out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have some code: using (var ctx = new testDataContext()) { var options = new
Have just started using Google Chrome , and noticed in parts of our site,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have a n-tire web application and search often times out after 30 secs. How
I've trawled the SO website thinking this is pretty obvious but alas I have
Have converted devise new session from erb to Haml but doens't work, this is
Ok, I have a treeview which I am using to display a number of
have an an array String classname[]={'a','b','c','d'}; ArrayAdapter<CharSequence> adapterClasses = new ArrayAdapter<CharSequence>( getApplicationContext(), R.layout.spinner_item_class, R.id.spinnerclasstxt,
I have trawled through tens of forums now to find MySql syntax to insert
I've trawled the site and the net and have tried various recursive functions etc

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.