Moshe S.
Moshe S.

Reputation: 3042

How can I open a new browser TAB?

I am trying to write a function that will receive a "browser instance" (and by that I mean webdriver.Firefox()) and will open a new tab with a certain URL.

This is the function:

def open_New_TAB(BrowserInstance, URL):
    if URL_Validation(URL):
        script = "window.open('" + URL + "')"
        BrowserInstance.execute_script(script)
    else:
        print('Invalid URL')
        return

URL_Validation returns True if the URL is valid

BrowserInstance is a return out of the following function, which only returns a "browser instance" of a certain browser type after the get(URL) was done:

def Open_Browser (URL=None, browserType='FF', Browser_Wait=20, Hide=False, 

    Maximize=False, Retries=3):

    if browserType == 'FF' or browserType == 'FireFox' or browserType == 'Firefox' or browserType == 'firefox':
        # FireFox Parameters
        profile = webdriver.FirefoxProfile()
        profile.accept_untrusted_certs = False
        browser = webdriver.Firefox(firefox_profile = profile, executable_path='G:\\Python\\geckodriver-v0.19.1-win64\\geckodriver.exe')

    elif browserType == 'Chrome' or browserType == 'chrome':
        # Chrome Parameters
        browser = webdriver.Chrome(executable_path="G:\\Python\\chromedriver_win32\\chromedriver.exe")

    elif browserType == 'IE' or browserType == 'Ie' or browserType == 'Explorer':
        browser = webdriver.Ie(executable_path="G:\\Python\\IEDriverServer_x64_3.8.0\\IEDriverServer.exe")

    else:
        print('No such browser type')

    browser.implicitly_wait(Browser_Wait) # Implicit wait
    browser.set_page_load_timeout(20) # Set the Timeout for waiting till the page loads

    for attempt in range(Retries):
        try:
            browser.get(URL)
        except TimeoutException:
            print("Timeout Expired at the:", attempt+1,'attempt')
    #         browser.close()
            continue   
    return browser

I was expecting that the open_New_TAB will open a new TAB in the same browser window that was opened by the Open_Browser function after running the following script:

browser = Open_Browser('http://10.0.1.76')
open_New_TAB(browser, 'http://10.0.7.131')

But, unfortunately the result was that a new browser window was opened for 'http://10.0.7.131'.

i don't understand why i got a new window instead of a new TAB because when I use the following script i do get the new TAB. And this script is exactly the same but without the function

 driver = webdriver.Firefox()
 driver.get("https://10.0.1.76")
 driver.execute_script("window.open('https://10.0.1.76')")

I would appreciate it very much if someone will tell me what I am doing wrong. It is OK if you have another way to accomplish the task of opening a new TAB. By the way I have tried to use: driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't') but without success (I didn't get a new TAB nor an exception)

Upvotes: 1

Views: 885

Answers (2)

S. Van den Wyngaert
S. Van den Wyngaert

Reputation: 691

As MivaScott said

It's called browser.link.open_newwindow. A value of 2 is off and a value of 3 in on. You can try setting the value via a profile and see what happens.

This is correct but as far as I know it is impossible to change this value since it is a frozen value. I tried this before and that's what I concluded. I switched to chrome to work around this issue, but ofcourse if you are bound to use firefox I am afraid you will need to do something like downloading the firefox driver source code, manually set this hard coded value to the correct value (2, I assume) and build the webdriver from that edited source.

Upvotes: 1

MivaScott
MivaScott

Reputation: 1806

You are getting a new window because you are asking for a new window

The JavaScript you are using says it right in the command. There is a setting in Firefox to open new windows in tabs, but I'm not sure if it will work in Selenium testing.

It's called browser.link.open_newwindow. A value of 2 is off and a value of 3 in on. You can try setting the value via a profile and see what happens.

Upvotes: 1

Related Questions