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:
driver.get("https://devqa.io")
and
driver.navigate().to("https://devqa.io")
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;
import org.openqa.selenium.chrome.ChromeDriver;
public class WebDriverJSExecutor {
private static String url = "https://devqa.io";
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
((JavascriptExecutor)driver).executeScript("window.location = \'"+url+"\'");
}
}