How to Use WebDriver Javascript Executor to Navigate to a URL

Selenium WebDriver provides methods to navigate to a url; these are driver.get() and driver.navigate().to().

For example:




There is also another way to navigate to a url and that is by using the WebDriver’s Javascript Executor, as shown in this example.

WebDriver - Navigate to URL Using JavaScript

Using window.location:

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;

public class WebDriverJSExecutor {

    private static String url = "";

    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        ((JavascriptExecutor)driver).executeScript("window.location = \'"+url+"\'");