Python Forum

Full Version: Firefox Selenium (open new tab)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
can any one tell me what are the methods to work with multiple tab in firefox using selenium & Py 3?
I have tried to open a new tab which is not happening ?

here are my code:
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import requests

browser = webdriver.Firefox()
browser.get('http://www.google.com')
browser.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')

search = browser.find_element_by_name('q')
i had tried this code but didnt worked, Am i doing anything wrong in this ??
One way to do it is with javascript.

browser.execute_script("window.open();")
#or to open a url
#browser.execute_script("window.open('{}');".format(ulr))
Each tab has an identifier(handle), to list all handles use:
when new tab is opened its handle is appended to the list.


browser.window_handles
To access current handle(the tab where current code will execute) use:

handle=browser.current_window_handle

To switch to desired window use:


browser.switch_to.window(handle)