Selenium Java to generate alert and navigate to another page using JavascriptExecutor
KW.java
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

class KW
{
    public static void main(String[] args) 
    {   
        System.setProperty("webdriver.chrome.driver", "/home/kodingwindow/drivers/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://kodingwindow.com/");
        
        JavascriptExecutor jse = (JavascriptExecutor)driver;
        jse.executeScript("window.location = 'https://kodingwindow.com/testapp/'");
        
        jse.executeScript("alert('Navigated from home page');");
        
        //driver.close();
    }
}
Advertisement