Exception in thread "main" java.lang.IllegalStateException: The driver executable must exist: C:\chromedriver.exe
at org.openqa.selenium.internal.Require$FileStateChecker.isFile(Require.java:315)
at org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:144)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:139)
at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:38)
at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:231)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:434)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:127)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:46)
at base.main(base.java:35)
Hi Guys was trying to do some practice but always this error comes up it will be really helpful if you guys can help me to understand the error. I tried setting up the selenium driver path but still error not going away. the code is on the bottom.Thank you in advance
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class base {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
WebDriver driver=new ChromeDriver();
//driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
WebDriverWait w =new WebDriverWait(driver,5);
String[] itemsNeeded= {"Cucumber","Brocolli","Beetroot"};
driver.get("https://rahulshettyacademy.com/seleniumPractise/");
Thread.sleep(3000);
addItems(driver,itemsNeeded);
driver.findElement(By.cssSelector("img[alt='Cart']")).click();
driver.findElement(By.xpath("//button[contains(text(),'PROCEED TO CHECKOUT')]")).click();
w.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.promoCode")));
driver.findElement(By.cssSelector("input.promoCode")).sendKeys("rahulshettyacademy");
driver.findElement(By.cssSelector("button.promoBtn")).click();
//explicit wait
w.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("span.promoInfo")));
System.out.println(driver.findElement(By.cssSelector("span.promoInfo")).getText());
}
public static void addItems(WebDriver driver,String[] itemsNeeded)
{
int j=0;
List<WebElement> products=driver.findElements(By.cssSelector("h4.product-name"));
for(int i=0;i<products.size();i )
{
//Brocolli - 1 Kg
//Brocolli, 1 kg
String[] name=products.get(i).getText().split("-");
String formattedName=name[0].trim();
//format it to get actual vegetable name
//convert array into array list for easy search
// check whether name you extracted is present in arrayList or not-
List itemsNeededList = Arrays.asList(itemsNeeded);
if(itemsNeededList.contains(formattedName))
{
j ;
//click on Add to cart
driver.findElements(By.xpath("//div[@class='product-action']/button")).get(i).click();
if(j==itemsNeeded.length)
{
break;
}
}
}
}
}
CodePudding user response:
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
defines that the chrome driver binary is located at C:\chromedriver.exe
. But the exception says the file does not exists there.
So just download the chrome driver from https://chromedriver.chromium.org/downloads and move rename it to C:\chromedriver.exe
and it will work.
CodePudding user response:
This error message...
Exception in thread "main" java.lang.IllegalStateException: The driver executable must exist: C:\chromedriver.exe
...implies that the program was unable to find the ChromeDriver executable in the specified location.
Solution
You need to take care of a couple of things here as follows:
Ensure that you have downloaded the exact format of the ChromeDriver binary from the download location pertaining to your underlying OS among:
- chromedriver_win32: For Windows OS
- chromedriver_mac64: For MAC OS X
- chromedriver_linux64: For Linux OS
Instead of storing the ChromeDriver executable within
C:\
drive, keep it within a directory, e.g.C:\BrowserDrivers\
. So effectively your line of code will be:System.setProperty("webdriver.chrome.driver", "C://BrowserDrivers//chromedriver.exe");
Ensure that ChromeDriver binary have executable permission for the non-administrator user.
Execute your Test as a non-administrator user.