This post will discuss how we can open a URL using Selenium Webdriver in a new window. We will discuss the solution for two versions of selenium ( Selenium 4 and Selenium 3 ).
Let’s discuss them one by one.
Open a URL in a new window using Selenium 4
We will use the selenium 4.1.1 version in our example.
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.1.1</version>
</dependency>
Selenium 4 has introduced a function newWindow() which can help open a new window.
We can use the below line of code to open a new window using selenium 4.
driver.switchTo().newWindow(WindowType.WINDOW);
So, we will first open the https://testkru.com/Elements/TextFields URL and then open the https://google.com/ in a new window.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class CodekruTest {
@Test
public void test() {
// pass the path of the chromedriver location in the second argument
System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
// opening the url
driver.get("https://testkru.com/Elements/TextFields");
// opening a new window
driver.switchTo().newWindow(WindowType.WINDOW);
// opening another url in the new window
driver.get("https://google.com");
}
}
The above code will open a new window with the URL – https://google.com.
Open a URL in a new window using Selenium 3
Selenium 3 doesn’t have the newWindow() function, so we have to find a different way of doing it.
We can open a URL in the new window using the javascript code below.
window.open("https://google.com", "","window.innerWidth, window.innerHeight");
where window.innerWidth gets the width of the original window and window.innerHeight gets the height of the original window. So, the above code will open a URL in the new window with the same width and height as the original window.
JavascriptExecutor in selenium helps in executing the javascript code. So, we will use it to execute the above code to open the URL in the new window.
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class CodekruTest {
@Test
public void test() {
// pass the path of the chromedriver location in the second argument
System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
// opening the url
driver.get("https://testkru.com/Elements/TextFields");
// opening the url in the new tab
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.open(\"https://google.com\", \"\",\"window.innerWidth, window.innerHeight\");");
}
}
This is it. We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.