How to Use WebDriver Javascript Executor to Navigate to a URL using Postman?

PostmanREST APISOAP API

We can use the Selenium webdriver JavaScript Executor to navigate to a URL. Selenium can run JavaScript commands by using the executeScript method.

The parameters to be passed to the executeScript method to navigate to a URL is - window.location = \'"+s+"\'. Here, s is the variable which stores the link of the page to navigate.

Syntax

JavascriptExecutor js = (JavascriptExecutor) driver;
String s = "https://www.tutorialspoint.com/about/about_careers.htm";
String scrpt = "window.location = \'"+s+"\'";
js.executeScript(scrpt);

Code Implementation

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
public class LnkJSNavigate{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java      \\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      //implicit wait
      driver.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS);
      // Javascript Executor to open a link
      JavascriptExecutor js = (JavascriptExecutor) driver;
      String s = "https://www.tutorialspoint.com/about/index.htm";
      String scrpt = "window.location = \'"+s+"\'";
      js.executeScript(scrpt);
      System.out.println("Page navigated to:" + driver.getTitle());
      driver.close();
   }
}

Output

raja
Published on 25-Jun-2021 13:04:13
Advertisements