Selenium will not open a new URL in a new tab (Python & Chrome)

I want to open multiple urls on different tabs using Selenium WebDriver and Python.

I'm 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 was looking for tutorials and it seems to me that this code should do what I want. In fact, the browser opens, url1 opens, as you would expect, a new tab opens as it should, but url2 is then loaded into the original tab instead of the new one (although the new tab is active).

(I use Chrome because when using Firefox I cannot get it to load any URLs at all. Firefox opens, but does not receive the requested url. I tried to find a solution for this, but to no avail).

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

Thank you for your help!

+7
python google-chrome selenium
source share
3 answers

There is an error in ChromeDriver that prevents ctrl / command + T from working:

What you can do as a workaround is to open the link in a new tab, and then switch to a new window using 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() will be executed on the newly opened tab.

+8
source share

Here is a simple platform-independent method:

The code:

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

Switch to the original tab:

The code:

 driver.switch_to_window(driver.window_handles[0]) 

Check the current title to make sure you are on the right page:

The code:

 driver.title 

For everything else, have fun!

+9
source share

An alternative way to open a new window is to use JavaScript and a 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 at runtime, it will look like there is focus in the new window, but for webdriver it is not. Do not be fooled by the visual. Also, do not forget to select a new window handler when closing the tab, as it will set 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 this error if you try to do something with the driver at this point.

+5
source share

All Articles