Selenium WebDriver Python Problem – Selenium won’t open a new URL in a new tab in Chrome

Selenium won’t open a new URL in a new tab in Chrome is an error which is a bug in ChromeDriver that prevents the ctrl key from working.

In this article I am going to explain what happens when you get this error and how you can solve it with a main solution, we will also explore other solutions which can possibly solve the issue.

Explaining Selenium WebDriver Python Problem – Selenium won’t open a new URL in a new tab in Chrome

First of all we need to understand why the error happens at all, the error is caused by a bug in ChromeDriver that prevents the ctrl key from working.

To reproduce the error you can run a simple Selenium script.

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

To solve the problem above, I have a couple of solutions which have worked for me, bellow is a detailed explanation of the solutions.

Solution 1 : use switch_to.window().

The error is caused by a bug in ChromeDriver, the bug stopps ctrl/command+T from working.

The first solution is to start by opening a link in a new tab and then use switch_to.window() in order to switch to a new window.

The example bellow shows you how this can be done.

                                                                       #
driver = webdriver.Chrome()
driver.get("https://www.google.com")
actions = ActionChains(driver)                     # we open a link in a new window
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]) # we switch to the window
driver.get("https://www.twitter.com")
                                                                       #

I hope this works, you can also try the solution bellow.

Solution 2 : use .execute_script().

The second solution is to use .execute_script().

You can do that using the line of code bellow.

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

Then you can switch back to the first tab using .switch_to_window().

                                                                       #
driver.switch_to_window(driver.window_handles[0])
                                                                       #

If this solution did not work for you, you can try the next and final solution.

Solution 3 : use driver.maximize_window().

The third and final solution is to maximize chrome, you can do that by using driver.maximize_window()

The snippet of code bellow is a demonstration of how you can use driver.maximize_window().

                                                                       #
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()
                                                                       #

It is worth to note that key_down(Keys.CONTROL) holds the ctrl key down.

All the above solutions were excellent to solve this problem. I wish you good luck.

The error above was hard to deal with, I spent hours looking for a proper solution or set of solutions.

Summing-up

The end of this article is here, please make sure to support the team by clicking the Kofi button, it is the red button on the top of this page, it is for donations and will help us help more developers in our error solving journey.

Please keep learning and keep coding, cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/