{"id":1096,"date":"2017-02-14T14:00:51","date_gmt":"2017-02-14T14:00:51","guid":{"rendered":"https:\/\/loadfocus.com\/blog\/?p=1096"},"modified":"2024-02-26T08:07:31","modified_gmt":"2024-02-26T08:07:31","slug":"how-to-open-a-new-tab-using-selenium-webdriver-with-java","status":"publish","type":"post","link":"https:\/\/loadfocus.com\/blog\/2017\/02\/how-to-open-a-new-tab-using-selenium-webdriver-with-java","title":{"rendered":"How to Open a New Tab using Selenium WebDriver with Java?"},"content":{"rendered":"<span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> 2<\/span> <span class=\"rt-label rt-postfix\">minutes read<\/span><\/span><p class=\"lead\">If you want to open a new tab in the browser with Selenium WebDriver and Java, below we&#8217;ve listed some working examples. Have in mind that at the moment, Selenium WebDriver has no build-in ability to open new tabs or new windows, and because of this we have to force the browser to open the links in new tabs or in new windows. This may be implemented in a later version of Selenium WebDriver.<\/p>\n<h3>1. Open an existent link a a new tab using Selenium WebDriver and Actions<\/h3>\n<p>More details on <a href=\"https:\/\/loadfocus.com\/blog\/2013\/09\/05\/how-to-locate-web-elements-with-selenium-webdriver\/\">how to locate web elements with Selenium WebDriver<\/a>.<\/p>\n<pre class=\"lang-java\"><code>\n\/\/identify the link in the page, that you want to open in a new tab of your browser's instance\nWebElement link = driver.findElement(By.cssSelector(linkLocator));\n\n\/\/this action will click the WebElement with the link identified above, hold CONTROL Key (or COMMAND if you're using Mac), click the WebElement and relese the Keys.CONTROL (use Keys.COMMAND if you're using Mac).\nnew Actions(driver)\n.keyDown(Keys.COMMAND)\n.click(link)\n.keyUp(Keys.COMMAND)\n.build()\n.perform();\n<\/code><\/pre>\n<p>The code above works in all browsers.<\/p>\n<h3>2. Open an existent link a a new tab using Selenium WebDriver with CONTROL\/COMMAND Key and T<\/h3>\n<p>There is also another option that may not work in Chrome browsers at the moment because of a limitation in the way keyboard inputs are simulated in ChromeDriver. Keys are sent directly to the render process, so any keyboard shortcut in the browser process will not be invoked by sendKeys() method.<\/p>\n<h3>See more details<\/h3>\n<p><script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<!-- Square --><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block;\" data-ad-client=\"ca-pub-8151016364557535\" data-ad-slot=\"3756710829\" data-ad-format=\"auto\" data-full-width-responsive=\"true\"><\/ins><br \/>\n<script><br \/>\n     (adsbygoogle = window.adsbygoogle || []).push({});<br \/>\n<\/script><\/p>\n<pre class=\"lang-java\"><code>\nString selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,\"t\");\n<\/code><\/pre>\n<p>Keys chord(CharSequence&#8230; value) method &#8211; Simulates pressing many keys at once in a &#8220;chord&#8221;. Takes a sequence of Keys.XXXX or strings; appends each of the values to a string, and adds the chord termination key (Keys.NULL) and returns the resultant string. Note: When the low-level webdriver key handlers see Keys.NULL, active modifier keys (CTRL\/ALT\/SHIFT\/etc) release via a keyup event.<\/p>\n<p>The code below will open the link in new Tab.<\/p>\n<pre class=\"lang-java\"><code>\ndriver.findElement(By.cssSelector(\"body\")).sendKeys(selectLinkOpeninNewTab);\n<\/code><\/pre>\n<p>Then when you open a new tab in your browser, you have to switch to it to be able to work with the newly opened tab&#8217;s elements:<\/p>\n<pre class=\"lang-java\"><code>\nList tabs = new ArrayList&amp;amp;amp;amp;amp;lt;&amp;amp;amp;amp;amp;gt;(driver.getWindowHandles());\ndriver.switchTo().window(tabs.get(1));\n<\/code><\/pre>\n<p>In order to switch back to the first tab, Selenium WebDriver offers the ability to switch back to the first tab, by executing the following code:<\/p>\n<pre class=\"lang-java\"><code>\ndriver.switchTo().defaultContent();\n<\/code><\/pre>\n<p>Quite often you may want to open a new tab in the same browser window that is running your Selenium WebDriver tests. Instead of opening a new browser, you can simply use the code below to open a new tab in the same browser:<\/p>\n<pre class=\"lang-java\"><code>\nString selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,\"t\");\ndriver.findElement(By.linkText(\"urlLink\")).sendKeys(selectLinkOpeninNewTab);\n<\/code><\/pre>\n<p>You&#8217;d need to keep track of the window name(s) or ids to allow switching between them.<\/p>\n<h3>3. Open an existent link a a new window using Selenium WebDriver<\/h3>\n<pre class=\"lang-java\"><code>\n\/\/identify the link in the page, that you want to open in a new tab of your browser's instance\nWebElement link = driver.findElement(By.cssSelector(linkLocator));\n\n\/\/this action will click the WebElement with the link identified above, hold SHIFT Key , click the WebElement and relese the Keys.SHIFT .\nnew Actions(driver)\n.moveToElement(link)\n.keyDown(Keys.SHIFT)\n.click()\n.keyUp(Keys.SHIFT)\n.build()\n.perform();\n<\/code><\/pre>\n<h4>How fast is your website comparing to your competitor&#8217;s? <a href=\"https:\/\/loadfocus.com\/website-speed-test\" data-wplink-edit=\"true\">Check now!<\/a><\/h4>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> 2<\/span> <span class=\"rt-label rt-postfix\">minutes read<\/span><\/span>If you want to open a new tab in the browser with Selenium WebDriver and Java, below we&#8217;ve listed some working examples. Have in mind that at the moment, Selenium WebDriver has no build-in ability to open new tabs or new windows, and because of this we have to force the browser to open the&#8230;  <a href=\"https:\/\/loadfocus.com\/blog\/2017\/02\/how-to-open-a-new-tab-using-selenium-webdriver-with-java\" class=\"more-link\" title=\"Read How to Open a New Tab using Selenium WebDriver with Java?\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29,48,16],"tags":[198,197,199],"class_list":["post-1096","post","type-post","status-publish","format-standard","hentry","category-selenium-webdriver","category-test-automation","category-ui-testing","tag-open-link-in-new-tab-or-window","tag-open-new-tab-in-browser-with-selenium-webdriver","tag-open-new-window-in-webdriver"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/posts\/1096","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/comments?post=1096"}],"version-history":[{"count":2,"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/posts\/1096\/revisions"}],"predecessor-version":[{"id":2979,"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/posts\/1096\/revisions\/2979"}],"wp:attachment":[{"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/media?parent=1096"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/categories?post=1096"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/tags?post=1096"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}