Published 04 Sep, 2022

Java - how to Pass command line argument ( baseURL) using java for selenium WebDriver

Category Java
Modified : Sep 29, 2022
34

i have created a runnable jar for my Selenium webDriver suite. now i have to test this in multiple base URL,As the baseURl will change according to the need. Can somebody please guide me through this. Is it possible to send command line arguments to Selenium Web Driver driver.get(baseUrl)

This code i have written but doesn't work.

public class news {

    public static void main(String[] args) {
        FirefoxDriver driver = new FirefoxDriver();

        try{
              JavascriptExecutor js = (JavascriptExecutor)driver;
              js.executeScript("var pr=prompt('Enter your URL please:',''); alert(pr);");  
              Thread.sleep(15000L);
              String URL = driver.switchTo().alert().getText();
              driver.switchTo().alert().accept();
              System.out.println(URL);
              driver.get(URL);
              System.out.println(URL);
            }catch(Throwable e)
              {
              System.out.println("failed");
            }
        driver.manage().window().maximize();

        WebElement login=driver.findElement(By.cssSelector("a[data-target='#login-box']"));
        login.click();
        driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
        driver.findElement(By.id("uname_h")).sendKeys("[email protected]");
        driver.findElement(By.id("password_h")).sendKeys("XYZ");
        WebElement login_button=driver.findElement(By.id("health_btn"));
        login_button.click();
       // driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
        driver.findElement(By.className("glyphicon-publish-news-events")).click();
        driver.findElement(By.xpath("/html/body/div[4]/div[4]/div[26]/div/ul/li[2]/a")).click();
        driver.findElement(By.id("newstitle")).sendKeys("Automation testing news");
        driver.findElement(By.id("newsdetails")).sendKeys("Automation testing news datails");
        WebElement webElement13 = driver.findElement(By.id("s2id_autogen3"));
        webElement13.sendKeys("ganesh");
        webElement13.sendKeys(Keys.ENTER);
}
}

Answers

There are 1 suggested solutions here and each one has been listed below with a detailed description. The following topics have been covered briefly such as Java, Selenium, Selenium Webdriver. These have been categorized in sections for a clear and precise explanation.

8

You can add below code:-

Scanner scanner = new Scanner(System.in);

    //  prompt for the URL
    System.out.print("Enter your URL: ");

    // get their input as a String
    String URL = scanner.next();

Now url will be asked in terminal

Now pass this URL variable in you below line code:-

  driver.get(URL);

My below working code:-

  WebDriver driver=null;

Scanner scanner = new Scanner(System.in);

    //  prompt for the URL
    System.out.print("Enter your URL: ");

    // get their input as a String
    String URL = scanner.next();



    System.out.println( URL );
    final FirefoxProfile firefoxProfile = new FirefoxProfile();
    driver = new FirefoxDriver(firefoxProfile);
    driver.manage().window().maximize();

    driver.get(URL);

Hope it will help you :)