0

I get this exception but i dont have geckodriver present in project window.

how to resolve? help appreciated thanks. my code:

System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

I had geckodriver.exe before, it wasn't working so tried removing it. now getting this new error.

Yuichi
  • 11
  • 3

2 Answers2

1

You need to provide full path of the geckodriver.exe, not just the driver file name.

Something like:

System.setProperty("webdriver.gecko.driver", "C:\\fullpath\\..\\geckodriver.exe");

If you are using Selenium v4.6.0 or higher version, you don't need to set the path of driver. You can get rid of the line System.setProperty. Code can be just:

WebDriver driver = new FirefoxDriver();
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

Refer this answer - https://stackoverflow.com/a/76463081/7598774

Shawn
  • 4,064
  • 2
  • 11
  • 23
  • The problem is i dont have geckodriver in my system but its still showing as "located". isnt that what the error means? that it is present but invalid? – Yuichi Aug 02 '23 at 13:23
  • Ok, i do have geckodriver but it is not in the same drive. it is in the selenium_projects directory in another hard disk. not sure if this matters but yeah – Yuichi Aug 02 '23 at 13:26
  • Which version of selenium do you use? – Shawn Aug 02 '23 at 13:27
  • 4.11.0 the newest version – Yuichi Aug 02 '23 at 13:29
  • So i deleted the other places where geckodriver was present. Also removed System.setProperty(). now getting this: Exception in thread "main" org.openqa.selenium.remote.NoSuchDriverException: Unable to obtain: Capabilities {acceptInsecureCerts: true, browserName: firefox, moz:debuggerAddress: true, moz:firefoxOptions: {}}, error Failed to run command: [C:\Users\hp\AppData\Local\Temp\selenium-manager814107484931005083599645405677712\selenium-manager.exe, --browser, firefox, --output, json] Build info: version: '4.11.0', revision: '040bc5406b' – Yuichi Aug 02 '23 at 13:33
  • I figured it out, thanks to your answer! After removing the System.setProperty(), i kept getting windows defender alerts whenever i ran the program. So i disabled windows defender, now firefox works with selenium, Thanks again – Yuichi Aug 02 '23 at 13:42
  • The original issue in your question `NoSuchDriverException: geckodriver located at geckodriver.exe, but invalid` has nothing to do with the issue you are facing now (related to Windows Defender). Your original issue has resolved and you are asking for solution for the second issue. This is not how it works in SO. My suggestion, ask a new question detailing the exact issue, with relevant code trails and error trace, this way your question reaches wider community. Good luck. – Shawn Aug 02 '23 at 13:58
1

This error message...

org.openqa.selenium.remote.NoSuchDriverException: geckodriver located at geckodriver.exe, but invalid

...implies that Selenium was unable to locate the required GeckoDriver in the path mentioned by the key webdriver.gecko.driver through the System.setProperty() line, as the expectation is the key webdriver.gecko.driver would contain the full path to the GeckoDriver binary as follows:

System.setProperty("webdriver.gecko.driver", "C:\\full\\path\\to\\geckodriver.exe");

Using Selenium Manager

As you are using Selenium v4.11.0 you can remove the following line of code:

System.setProperty("webdriver.gecko.driver", "geckodriver.exe");

as Selenium Manager which is now fully integrated with Selenium will download the matching GeckoDriver silently in the background and execute the test.

Your effective code block will be:

WebDriver driver = new FirefoxDriver();
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://www.google.com/");
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352