Since Firefox does not support Control + T anymore for the tab, I started using driver.execute_script("window.open('URL', 'new_window')")
I am trying to display the title of the different tab I open and switch between them. For the example below, I expect the output to be facebook, google and back to facebook. Right now the output is facebook, facebook and facebook.
I tried the answer from here but it also did not work: Switch back to parent tab using selenium webdriver
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.facebook.com/")
print(driver.title)
driver.execute_script("window.open('http://google.com', 'new_window')")
print(driver.title)
driver.switch_to.window(driver.window_handles[0])
print(driver.title)
UPDATED: I tried the follow code and it still did not work.
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.facebook.com/")
print(driver.title)
window_before = driver.window_handles[0]
driver.execute_script("window.open('http://google.com', 'new_window')")
window_after = driver.window_handles[1]
driver.switch_to.window(window_after)
print(driver.title)
window.open
will open the link in a new tab. Selenium Firefox driver doesn't have the ability to switch between tabs as there is only one window handle for both of them (unlike Chrome which has 2). If you give thewindow()
command 'specs' parameter with width and height it will open a new window and you will be able to switch.After opening the new window the driver is still focused on the first one, you need to switch to the new window first.
A few words about Tab/Window switching/handling:
number_of_windows_to_be(num_windows)
before switching between Tabs/Windows.expected_conditions
astitle_contains("partial_page_title")
before extracting the Page Title.Here is your own code with some minor tweaks mentioned above:
Console Output:
I've used
driver.getWindowHandles();
to get all windows anddriver.switchTo().window(handle);
to switch to required one.