Selenium won't open a new URL in a new tab (Python & Chrome)
Asked Answered
T

4

11

I want to open quite a few URLs in different tabs using Selenium WebDriver & Python.

I am not sure what is going wrong:

driver = webdriver.Chrome()
driver.get(url1)
time.sleep(5)
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL+'t')
url2 = 'https://www.google.com'
driver.get(item2)

I looked up tutorials and it seems to me as though this code should do what I want. What actually happens is the browser opens, url1 opens as it should, a new tab opens as it should but url2 then loads in the original tab instead of the new one (even though the new tab appears to be the active one).

(I am using Chrome because when using Firefox I can't get it to load any URLs at all. Firefox opens but does not get the url requested. I have tried to find a solution to this but to no avail.)

Is there anything I can change in my code to get the new URL to open in the new tab?

Thanks for your help!

Thach answered 7/5, 2016 at 12:33 Comment(0)
S
13

There is a bug in ChromeDriver that prevents ctrl/command+T from working:

What you can do, as a workaround, is to open a link in a new tab and then switch to a new window using the switch_to.window(). Working sample:

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("https://www.google.com")

# open a link in a new window
actions = ActionChains(driver)
about = driver.find_element_by_link_text('About')
actions.key_down(Keys.CONTROL).click(about).key_up(Keys.CONTROL).perform()

driver.switch_to.window(driver.window_handles[-1])
driver.get("https://stackoverflow.com")

Now the last driver.get() would be performed in a newly opened tab.

Shaina answered 7/5, 2016 at 12:45 Comment(2)
thanks, but that bug seems to be about ctrl-t not opening a new tab at all. I can open a new tab fine but not load a url in that tab. I tried your code but I probably don't understand it correctly. There was an error on the find_element_by_link line (unable to locate element). I am loading an html page (a page source), I'm not sure if that makes a difference.Thach
@Thach okay, in your case it's just that you need to do driver.switch_to.window(driver.window_handles[-1]) after opening a new tab.Shaina
C
16

Here is a simple way, platform independent:

Code:

driver.execute_script("window.open('http://google.com', 'new_window')")

Switching back to the original tab:

Code:

driver.switch_to_window(driver.window_handles[0])

Checking the current title to be sure you are on the right page:

Code:

driver.title

For everything else, have fun!

Carmencarmena answered 6/9, 2016 at 16:36 Comment(6)
I am using Chrome webdriver and it actually opens in a new tab, thank you very muchHydrolyze
I am using Firefox v67.0.4 with geckodriver 0.24.0 and it opens a new window but NOT a new TabDollop
in case i want to open 2 new tabs, the code doesnt work, it opens only 1 new tab.Dockery
@baermathias, whether the code opens a new tab or a new window is based on the user settings.Meliorate
@SafwanSamsudeen How should the user settings be configurated?Dollop
@baermathias Depends on your browser. I don't use Firefox, so just Google 'How to configure new window as new tab in Firefox' or something like that.Meliorate
S
13

There is a bug in ChromeDriver that prevents ctrl/command+T from working:

What you can do, as a workaround, is to open a link in a new tab and then switch to a new window using the switch_to.window(). Working sample:

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("https://www.google.com")

# open a link in a new window
actions = ActionChains(driver)
about = driver.find_element_by_link_text('About')
actions.key_down(Keys.CONTROL).click(about).key_up(Keys.CONTROL).perform()

driver.switch_to.window(driver.window_handles[-1])
driver.get("https://stackoverflow.com")

Now the last driver.get() would be performed in a newly opened tab.

Shaina answered 7/5, 2016 at 12:45 Comment(2)
thanks, but that bug seems to be about ctrl-t not opening a new tab at all. I can open a new tab fine but not load a url in that tab. I tried your code but I probably don't understand it correctly. There was an error on the find_element_by_link line (unable to locate element). I am loading an html page (a page source), I'm not sure if that makes a difference.Thach
@Thach okay, in your case it's just that you need to do driver.switch_to.window(driver.window_handles[-1]) after opening a new tab.Shaina
C
9

An alternative way to open a new window is to use JavaScript and the window handler to switch between them.

driver = webdriver.Chrome()

# Open a new window
# This does not change focus to the new window for the driver.
driver.execute_script("window.open('');")

# Switch to the new window
driver.switch_to.window(driver.window_handles[1])
driver.get("http://stackoverflow.com")

# close the active tab
driver.close()

# Switch back to the first tab
driver.switch_to.window(driver.window_handles[0])
driver.get("http://google.se")

# Close the only tab, will also close the browser.
driver.close()

If you look at your browser while you're executing it will look like the new window has focus, but to the webdriver, it doesn't. Don't be fooled by the visual. Also remember to select a new window handler when you close a tab as it will set the driver.current_window_handle to

selenium.common.exceptions.NoSuchWindowException: 
    Message: no such window: target window already closed from unknown error: web view not found
  (Session info: chrome=<Your version of chrome>)
  (Driver info: chromedriver=<Your chrome driver version> (<string of numbers>),platform=<Your OS>)

on .close() and it will throw that error if you try to do stuff with the driver at that stage.

Carbajal answered 13/2, 2017 at 13:35 Comment(0)
S
0

you need to maximize your chrome for this

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.google.com/")

e = driver.find_element_by_tag_name("body")
ActionChains(driver).key_down(Keys.CONTROL).click(e).send_keys("k").key_up(Keys.CONTROL).perform()

here key_down(Keys.CONTROL) will hold down ctrl key, to get focus on page i am clicking body of the page, then click k

Squeal answered 15/3, 2019 at 5:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.