{"id":6717,"date":"2018-04-21T20:33:02","date_gmt":"2018-04-21T15:03:02","guid":{"rendered":"https:\/\/techbeamers.com\/?p=6717"},"modified":"2025-11-30T11:25:38","modified_gmt":"2025-11-30T16:25:38","slug":"switch-between-windows-selenium-python","status":"publish","type":"post","link":"https:\/\/techbeamers.com\/switch-between-windows-selenium-python\/","title":{"rendered":"Python Selenium Switch Between Windows"},"content":{"rendered":"\n<p>In this <a href=\"\/selenium-webdriver-python-tutorial\/\">Selenium Python tutorial<\/a>, we&#8217;ll learn to <strong>switch between windows<\/strong>. While working on a website, it is highly possible that we open a large number of windows.<\/p>\n\n\n\n<p>Each window may require us to perform some actions to complete an end-to-end flow. For this, we should be able to switch between them.<\/p>\n\n\n\n<p>We need to switch over the control also and then do the required operation, because, by default, the focus remains on the parent window.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-switch-between-windows-using-selenium-python-full-code\">Switch Between Windows Using Selenium Python &#8211; Full Code<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"\/wp-content\/uploads\/2018\/04\/Switch-Between-Windows-Selenium-Python.png\"><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"400\" src=\"\/wp-content\/uploads\/2018\/04\/Switch-Between-Windows-Selenium-Python.png\" alt=\"Switch Between Windows Selenium Python\" class=\"wp-image-6718\"\/><\/a><figcaption class=\"wp-element-caption\">Switch Between different Windows using Selenium Python API <\/figcaption><\/figure>\n<\/div>\n\n\n<p>The WebDriver library provides a simple method to address our need to shift from one browser window to another. However, there are two of them that you have to call accordingly.<\/p>\n\n\n\n<p>The newer one: <code><strong>switch_to.window(window_handle)<\/strong><\/code> and the older version: <code><strong>switch_to_window(window_handle)<\/strong><\/code> . The choice to call them depends on the version of Selenium you are using. You can call these from your code using the following syntax.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"1\">\n<li><code><strong>switch_to_window(window_handle)<\/strong><\/code>: This was the syntax used in older versions of Selenium (pre-3.0). If you are working with an older version of Selenium, you should use this syntax.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">win_handle = \"your_window_handle\"\r\nweb_driver.switch_to_window(win_handle)<\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><code><strong>switch_to.window(window_handle)<\/strong><\/code>: This is the correct syntax for newer versions of Selenium (3.0 and later). If you are using a relatively recent version of Selenium, it&#8217;s better to use this syntax.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">win_handle = \"your_window_handle\"\nweb_driver.switch_to.window(win_handle)<\/pre>\n\n\n\n<p>The Webdriver library has a system in place to inspect all calls coming from the browser and its windows.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-selenium-python-code-to-switch-between-windows\">Selenium Python code to switch between windows<\/h3>\n\n\n\n<p>In this section, we&#8217;ll demonstrate how easily you achieve the transitioning of Windows in the Firefox web browser.<\/p>\n\n\n\n<p>Now, refer to the example and the code to learn how it is working. In this, we used the newer method to switch between windows. You can do the same with Google Chrome, however, you will need to use the Chrome driver. We used some dummy websites to illustrate the desired workflow.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from selenium import webdriver\r\nfrom selenium.webdriver.support.ui import WebDriverWait\r\nfrom selenium.webdriver.support import expected_conditions as EC\r\nimport time\r\n\r\n# Set up the doj (driver)\r\ndoj = webdriver.Firefox()\r\ndoj.maximize_window()\r\n\r\n# Open the first website\r\ndoj.get(\"https:\/\/www.example.com\")\r\n\r\n# Get the first window handle\r\nw1 = doj.window_handles[0]\r\nt1 = doj.title\r\nprint(t1)\r\n\r\n# Open a new window for another website\r\ndoj.execute_script(\"window.open('https:\/\/www.example.org', 'new window')\")\r\n\r\n# Get the second window handle\r\nw2 = doj.window_handles[1]\r\n\r\n# Switch to the new window\r\ndoj.switch_to.window(w2)\r\n\r\n# Wait for the window to load\r\nWebDriverWait(doj, 10).until(EC.title_contains(\"New Window Title\"))\r\n\r\nt2 = doj.title\r\nprint(t2)\r\n\r\n# Compare titles\r\nif t1 != t2:\r\n    print('Switched to the second website as the titles do not match.')\r\nelse:\r\n    print('Control did not switch to the new window.')\r\n\r\n# Add a delay before switching back to the original window\r\ntime.sleep(3)\r\n\r\n# Switch back to the original window\r\ndoj.switch_to.window(w1)\r\n\r\n# Wait for the title of the original window to load\r\nWebDriverWait(doj, 10).until(EC.title_contains(\"Original Window Title\"))\r\n\r\n# Verify that the titles now match\r\nif t1 == doj.title:\r\n    print('Returned to the parent window. Titles now match.')\r\n\r\nprint(doj.title)\r\n\r\n# Close the doj\r\ndoj.quit()<\/pre>\n\n\n\n<p>In this example, we show you how to use Selenium with Python for web automation.<\/p>\n\n\n\n<p>Also Check: <a href=\"https:\/\/techbeamers.com\/python-switch-case-statement\/\" target=\"_blank\" rel=\"noreferrer noopener\">Using Switch Case in Python<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-about-the-code-to-switch-between-windows\">About the code to switch between windows<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Open the first window:<\/strong>\u00a0This is the first step in the script,\u00a0and it is straightforward.\u00a0The code opens a Firefox browser window and navigates to a dummy site <code class=\"\">example.com<\/code>.<\/li>\n\n\n\n<li><strong>Get the window handles:<\/strong>\u00a0This step is important because it allows the script to keep track of the windows that it has to use.<\/li>\n\n\n\n<li><strong>Open a new window:<\/strong>\u00a0This step opens a 2nd browser window and switches to the dummy site\u00a0<code class=\"\">example.org<\/code>\u00a0by calling<code class=\"\">execute_script()<\/code>\u00a0.<\/li>\n\n\n\n<li><strong>Get the handle of a new window:<\/strong>\u00a0This step is similar to step 2,\u00a0but it gets the window handle of the second window and stores it in the variable\u00a0<code class=\"\">win2<\/code>.<\/li>\n\n\n\n<li><strong>Switch to the new child window:<\/strong>\u00a0This step switches the focus of the script to the second window.\u00a0The code calls\u00a0<code class=\"\">switch_to.window()<\/code>\u00a0to do this.<\/li>\n\n\n\n<li><strong>Add an explicit wait:<\/strong>\u00a0This step is important because it ensures that the script does not continue until the second window has fully loaded.\u00a0The code uses the\u00a0<code class=\"\">WebDriverWait<\/code>\u00a0class to wait for this purpose.<\/li>\n\n\n\n<li><strong>Compare and verify that the window titles don&#8217;t match:<\/strong>\u00a0This step compares the titles of the two windows and prints a message if it fails.<\/li>\n\n\n\n<li><strong>Add a few seconds of wait before switching back:<\/strong>\u00a0This step adds a brief pause. It ensures that the script has enough time to switch back to the main window.<\/li>\n\n\n\n<li><strong>Switch back to the original window:<\/strong>&nbsp;This step switches the focus of the script back to the original window.&nbsp;The code uses the&nbsp;<code class=\"\">switch_to.window()<\/code>&nbsp;method to do this.<\/li>\n\n\n\n<li><strong>Add an explicit wait:<\/strong>\u00a0This step is similar to step 6 to ensure the script waits enough to move to the original window.<\/li>\n\n\n\n<li><strong>Verify the titles match:<\/strong>\u00a0This step compares the titles of the two windows again and prints a message if it fails.<\/li>\n\n\n\n<li><strong>Close the driver:<\/strong>&nbsp;This step closes the browser driver and quits the script.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-point-by-point-summary\">Point-by-point summary<\/h4>\n\n\n\n<p>Here is a point-by-point summary of your code, considering my last reply:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open the first window and get the window handle.<\/li>\n\n\n\n<li>Open a new window and get the window handle.<\/li>\n\n\n\n<li>Switch to the new window and wait for it to load. It is a good coding practice to use <a href=\"https:\/\/techbeamers.com\/webdriver-wait-commands-tutorial-examples\/\" target=\"_blank\" rel=\"noreferrer noopener\">WebDriverWait<\/a>.<\/li>\n\n\n\n<li>Compare the titles of the two windows to verify that the switch was successful.<\/li>\n\n\n\n<li>Wait a few seconds before switching back to the original window.<\/li>\n\n\n\n<li>Switch back to the original window and wait for it to load.<\/li>\n\n\n\n<li>Compare the titles of the two windows again to verify that the switch was successful.<\/li>\n\n\n\n<li>Close the browser driver.<\/li>\n<\/ul>\n\n\n\n<p>This example is like a starting point to learn web automation. It&#8217;s handy for testers, developers, and anyone who wants to automate web stuff. The code shows smart waiting, handling windows right, and checking things properly to make strong and dependable automated tests.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2>\n\n\n\n<p>It is essential to understand how to use <a href=\"https:\/\/techbeamers.com\/switch-between-iframes-selenium-python\/\" target=\"_blank\" rel=\"noopener\">Selenium Python to&nbsp;switch<\/a> between windows. You can re-use this technique to solve real-time use cases in your projects.<\/p>\n\n\n\n<p>Selenium makes it easy to work with multiple browser windows, tabs, and pop-ups. This guide helps you switch between them, interact with elements, and automate tasks. Keep practicing to become a skilled Selenium user!<\/p>\n\n\n\n<p>For more updates on Selenium Python tutorials, do follow us on our social media accounts. It will ensure you get free access to all our future resources.<\/p>\n\n\n\n<p><strong>Best,<\/strong><\/p>\n\n\n\n<p><strong>TechBeamers<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Selenium Python tutorial, we&#8217;ll learn to switch between windows. While working on a website, it is highly possible that we open a large number of windows. Each window may require us to perform some actions to complete an end-to-end flow. For this, we should be able to switch between them. We need to [&hellip;]<\/p>\n","protected":false},"author":321,"featured_media":6718,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","jetpack_post_was_ever_published":false},"categories":[92],"tags":[7471],"class_list":{"0":"post-6717","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python-programming-tutorials","8":"tag-python-selenium-tutorial"},"jetpack_featured_media_url":"https:\/\/techbeamers.com\/wp-content\/uploads\/2018\/04\/Switch-Between-Windows-Selenium-Python.png","_links":{"self":[{"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/posts\/6717","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/comments?post=6717"}],"version-history":[{"count":1,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/posts\/6717\/revisions"}],"predecessor-version":[{"id":23632,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/posts\/6717\/revisions\/23632"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/media\/6718"}],"wp:attachment":[{"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/media?parent=6717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/categories?post=6717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/tags?post=6717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}