Home > Enterprise >  updated correct chrome driver path/driver version and add selenium jar too, but still getting below
updated correct chrome driver path/driver version and add selenium jar too, but still getting below

Time:09-17

code :

package Demo1;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;


public class Chrome {

    public static void main(String[] args) {
  WebDriver driver= new ChromeDriver();
        
        System.setProperty("webdriver.chrome.driver","C:\\New folder\\chromedriver.exe");
        driver.get("https://www.youtube.com/watch?v=BtmeQOcdIKI");
        System.out.println(driver.getTitle());

    }

}

error :
Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html
    at com.google.common.base.Preconditions.checkState(Preconditions.java:847)
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:134)
    at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:35)
    at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:159)
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:355)
    at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:94)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
    at Demo1.Chrome.main(Chrome.java:9)

CodePudding user response:

You are setting the system property too late.

Looking at the stacktrace, the exception is being thrown while executing the following line of your code:

WebDriver driver= new ChromeDriver();

At that point, the line of your code that sets the system property hasn't been reached yet.

Evidently, you need to set the system property before creating the ChromeDriver object.

CodePudding user response:

The first line in your main method should be :

System.setProperty("webdriver.chrome.driver","C:\\New folder\\chromedriver.exe");

something like this.

public class Chrome {

  WebDriver driver = null;

  public static void main(String[] args) {
     System.setProperty("webdriver.chrome.driver","C:\\New folder\\chromedriver.exe");
     driver = new ChromeDriver();
     driver.get("https://www.youtube.com/watch?v=BtmeQOcdIKI");
     System.out.println(driver.getTitle());

    }

}

should get the job done.

  • Related