Error on simple class in selenium webdrive - java

Executing simple class
Enter code here
package lesson1;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Brf {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver(); //Launches Firefox Browser with blank url
driver.get("http://www.gcrit.com/build3/admin/login.php");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin#123");
driver.findElement(By.id("tdb1")).click();
String url = driver.getCurrentUrl();
if (url.equals("http://www.gcrit.com/build3/admin/index.php")){
System.out.println("Login Successful -Passed");
}
else
{
System.out.println("Login Unsuccessful -Failed");
}
driver.close(); //Closes the Browser
}
}
Getting the error:
Error on simple class in selenium webdrive:
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:335)

You are getting this error because you haven't set the path of gecko driver in your code.
Please set the path of gecko driver before creating a new instance of Firefox driver.
System.setProperty("webdriver.gecko.driver"," Path to geckodriver");
WebDriver driver = new FirefoxDriver();

Need to add driver path, if you are using selenium jars above 3.0 , if you are using selenium jars below 3.0 your code is good to go, but i guess you are using selenium jars above 3.0
Public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver"," Path to geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.gcrit.com/build3/admin/login.php");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin#123");
driver.findElement(By.id("tdb1")).click();
String url = driver.getCurrentUrl();
if (url.equals("http://www.gcrit.com/build3/admin/index.php")){
System.out.println("Login Successful -Passed");
}
else
{
System.out.println("Login Unsuccessful -Failed");
}
driver.close(); //Closes the Browser
}

The cause of the error is that you don't have a proper setup for detection of a GeckoDriver file named "geckodriver.exe" (this prevents you from being able to open Firefox with your program). In case you don't have such a file, you can download one h̲e̲r̲e̲ (I suggest getting the latest version).
With such a file, you can carry out one of the following two procedures to rectify your problem:
Method 1
Add the path of the folder containing the "geckodriver.exe" file to your PATH environment variable value (instructions on how to modify this variable value can be seen h̲e̲r̲e̲).
Method 2
Prepend the body of your main method with the following statement (where GECKODRIVER_PATH represents the path of the "geckodriver.exe" file):
System.setProperty("webdriver.gecko.driver", "GECKODRIVER_PATH");

Set the gecko driver path which is initialise the firefox driver.

Related

Exception in thread "main" java.lang.IllegalStateException:The path to the driver executable must be set by the : system property

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:199)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:109)
at org.openqa.selenium.chrome.ChromeDriverService.access$0(ChromeDriverService.java:1)
at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137) at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88) at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:116)
at practise_locators.DatePicker.main(DatePicker.java:11)
Here is my code:
package practise_locators;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class DatePicker {
public static void main(String[] args){
WebDriver driver = new ChromeDriver();
System.setProperty("WebDriver.Chrome.driver", "E:\\chromedriver.exe");
driver.get("https://www.google.com");
}
}
The error says it all :
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:199)
The following phrases from the error implies that there is an error in the line containing webdriver.chrome.driver
The error can be either of the following :
Error in the System Class Method setProperty()(including sequence) :
System.setProperty()
This line should be the very first line in your script.
Error in the specified Key :
"WebDriver.Chrome.driver"
Error in the Value field :
"E:\\chromedriver.exe"
You have to pass the absolute path of the WebDriver through either of the following options :
Escaping the back slash (\\) e.g. "C:\\path\\to\\chromedriver.exe"
Single forward slash (/) e.g. "C:/path/to/chromedriver.exe"
Your code seems to be having two issues as follows :
First issue is in specifying the Key which instead of "WebDriver.Chrome.driver" should have been "webdriver.chrome.driver" as follows :
System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
Second issue is in the sequence of mentioning the Key "webDriver.chrome.driver" in your program which should be before WebDriver driver = new ChromeDriver(); as follows :
System.setProperty("WebDriver.Chrome.driver", "E:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
I have seen many people are using wrong sequence.
the sequence should be.
First set the property and then launch the browser
System.setProperty("webdriver.chrome.driver", "F:/chrome/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.navigate().to("https://www.google.com");
Download the chromedriver version corresponding to the chrome version in your system from https://chromedriver.chromium.org/downloads . Unzip the file and run the below code in the IDE.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Selintroduction {
public static void main(String[] args) {
//Invoking browser
System.setProperty("webdriver.chrome.driver","C:\\Users\\HP\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
}
}
(Path would depend on your file location in the PC.)
I was facing the same error when my code was like so:
System.setProperty("webdriver.chrome.driver","C:\\Users\\abs\\chromedriver_win32.exe");
It works after adding "chromedriver" before ".exe" like so:
System.setProperty("webdriver.chrome.driver","C:\\Users\\abs\\chromedriver_win32\\chromedriver.exe");

Exception in thread "main" java.lang.IllegalStateException: The driver executable does not exist: /Users/Golcha/Desktop/Automation/geckodriver.exe

Getting the following error
Exception in thread "main" java.lang.IllegalStateException: The driver
executable does not exist:
/Users/Golcha/Desktop/Automation/geckodriver.exe
Code:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class selenium{
private static WebDriver driver;
public static void main(String[]args){
System.setProperty("webdriver.gecko.driver","Users/Golcha/Desktop/Automation/geckodriver.exe");
setDriver(new FirefoxDriver());
}
public static WebDriver getDriver() {
return driver;
}
public static void setDriver(WebDriver driver) {
selenium.driver = driver;
}
}
Try below code but check the path of geckodriver properly before executing the below code or I ll suggest you to paste geckodriver in C drive or any other drives to make your path simple as below one :
System.setProperty("webdriver.firefox.marionette", "C:\\geckodriver.exe");
It should work without any problem . All the best !
I thing you have not mentioned the complete(full) path for the Geckodriver in your System.setProperty() method. you have missed the drive letter "C"
"C:/Users/Golcha/Desktop/Automation/geckodriver.exe"
Let me know if this solved your problem.
I had a similar problem but weirdly I was not able to execute the selenium script on both my drivers (chrome and firefox), I tried checking my setProperty method, my gecko driver path and chrome driver path. Lastly, I tried updating both my browser and it seems to be working now. (my current version of Chrome is 66 and my current version of my Firefox is 56.0).

java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property

package com.merchantPlatform;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class MerchantPlatformTest {
public static void main(String[] args) {
System.getProperty("webdriver.gecko.driver", "C:\\Selenium WebDriver\\geckodriver\\geckodriver-v0.17.0-win64\\geckodriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
// Initialize WebDriver
WebDriver driver = new FirefoxDriver(capabilities);
/* This works fine for versions lesser than Selenium 3. For Selenium 3 and higher, it will throw java.lang.IllegalStateException */
// Maximize Window
driver.manage().window().maximize();
// Wait For Page To Load
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
// Navigate to MerchantPlatform URL
driver.get("http://localhost:52939/");
}
}
Error
I am getting the below exception with System.getProperty
Exception in thread "main" java.lang.IllegalStateException: The path
to the driver executable must be set by the webdriver.gecko.driver
system property; for more information, see
https://github.com/mozilla/geckodriver. The latest version can be
downloaded from https://github.com/mozilla/geckodriver/releases
at com.google.common.base.Preconditions.checkState(Preconditions.java:738)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124)
at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:41)
at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:115)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:330)
at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:207)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:108)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:137)
at com.merchantPlatform.MerchantPlatformTest.main(MerchantPlatformTest.java:20)
You have to use System.setProperty not the System.getProperty as follows.
System.setProperty("webdriver.gecko.driver",
"C:\\Selenium WebDriver\\geckodriver\\geckodriver-v0.17.0-win64\\geckodriver.exe");
I have noticed that you are using wrong syntax, to opening the browser.
Instead of using System.getProperty, You have to use System.setProperty as mentioned below.
System.setProperty("webdriver.gecko.driver", "C:\\Selenium WebDriver\\geckodriver\\geckodriver-v0.17.0-win64\\geckodriver.exe");
For more details on this issue, refer this page.

Unable to get URL in mozilla in selenium webdriver

I am using Mozilla 51.0.1 and Eclipse Lunar 3.0.1. When I am trying to run my code I am not able to get URL in the browser, it just opens.
package SessionPack;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class SessionCalss {
public static void main(String arg[])
{
System.out.println("String");
WebDriver driver;
System.setProperty("webdriver.gecko.driver", "C:\\New folder\\geckodriver.exe");
driver=new FirefoxDriver();
driver.get("www.gooogle.com");
}
}
Your request is wrong in two places:
You write extra character "o" - gooogle
You have to added https:// before URL-adress - https://www.google.com
After fix this code will be work.
You have to specify http:// or https:// specifically; you have to change your URL string from:
www.gooogle.com to https://www.gooogle.com

Browser opens, but never goes to URL using Selenium

I want to write couple of tests for a web page with Selenium and jUnit4, but I can't figure out how to make Firefox open the URL I need. Without System.setProperty(...) I'm getting Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. and browser never opens. Yet if I implement it the browser does open on a default start "new page", but the line driver = new FirefoxDriver(); and further never executes.
Below is the simplest code version of what I'm trying to achieve:
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Main {
static String URL = "http://www.google.com";
static WebDriver driver;
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
//Following code never executes
driver = new FirefoxDriver();
//I'm not sure if this is how I'm supposed to open URL, but I never had this code executed.
driver.get(URL);
driver.quit();
}
}
UPDATED:
These links were helpful to solve the problem of proper geckodriver installation.
https://github.com/mozilla/geckodriver/releases
http://learn-automation.com/use-firefox-selenium-using-geckodriver-selenium-3/
Actually you need to set geckodriver.exe path instead of firefox.exe in this
System.setProperty("webdriver.gecko.driver", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
So just replace geckodriver.exe path and try

Categories

Resources