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
This is my code.
package newpackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class MyClass {
public static void main(String[] args) {
//declaration and instatiation of objects/variables
WebDriver driver = new FirefoxDriver();
String baseUrl = "http://newtours.demoaut.com";
String expectedTitle = "Welcome: Mercury Tours";
String actualTitle ="";
//launch firefox and direct it to the URL
driver.get(baseUrl);
//get the actual value of the title
actualTitle = driver.getTitle();
if (actualTitle.contentEquals(expectedTitle)){
System.out.println("Test Passed");
}else {
System.out.println("Test failed");
}
//close firefox
driver.close();
//exit the program
System.exit(0);
}
}
For selenium 3.0 and above it is mandatory to specify gecko.driver path. Add below lines to your code before initializing driver and error will not appear.
System.setProperty("webdriver.firefox.marionette","xxx\geckodriver.exe");
//xxx - path to geckodriver.exe file
WebDriver driver = new FirefoxDriver();
Related
I have the following Java code for a selenium web driver in JMeter:
package com.jmeter.test.scripts;
import java.io.File;
import org.testng.annotations.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.OutputType;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.TakesScreenshot;
#Test
public class SnapScreenshot
{
String fileName = UUID.randomUUID.toString();
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "C:\\Apache\\apache-jmeter-5.5\\Drivers\\Chrome\\109\\chromedriver.exe");
System.setProperty("webdriver.chrome.logfile", "C:\\Temp\\driverlog.log");
System.setProperty("webdriver.chrome.verboseLogging", "true");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://www.google.com/");
Thread.sleep(5000);
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
File destFile = new File("C:\\temp\\screenshots\\" + fileName + ".png");
try
{
FileUtils.copyFile(srcFile, destFile);
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
driver.quit();
}
}
}
It doesn't appear to be executing:
The logfile is not created
Any System.out.println() statements I add are not executed
I'm also getting this error message: ERROR c.g.j.p.w.s.WebDriverSampler: unknown protocol: data
Not sure what I'm doing wrong. I do have the Chrome web driver configured via "jp#gc - Chrome Driver Config" but this doesn't seem to matter much.
If you're using the WebDriver Sampler you need to amend your code accordingly:
In the Chrome Driver Config specify the path to your chromedriver executable
Remove ChromeDriver instance initialization and use WDS.browser wherever you need to manipulate the browser
Switch to "groovy" language
Why did this happen? Chrome opens but the URL does not open with the code.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Id {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "E:\\chromedriver\\chromedriver.exe"); //open browser
WebDriver driver = new ChromeDriver();
**driver.get("google.co.in"); //open URL**
driver.findElement(By.name("q")).sendKeys("Quadkast" + Keys.ENTER);
}
}
You need to give http/https protocol along with the url in the get() method
driver.get("http://www.google.com");
or
driver.get("https://google.com");
I'm trying to launch firefox browser from eclipse using selenium as I'm learning selenium.
My tutor wrote the below code but when i'M trying the same code I get this exception-
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
Link1. The latest version can be downloaded from
Link2
Code:
package appselenium1;
import org.openqa.selenium.firefox.FirefoxDriver;
public class A {
public static void main(String[] args) {
FirefoxDriver driver = new FirefoxDriver();
driver.get("http://www.gmail.com");
}
}
You are facing this exception, because you have not used gecko driver , which is required for launching and sending command in selenium.
You can download the latest version of gecko version from here
Try this :
package appselenium1;
import org.openqa.selenium.firefox.FirefoxDriver;
public class A {
static WebDriver driver ;
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Downloads\\geckodriver-v0.20.1-win64\\geckodriver.exe");
driver = new FirefoxDriver();
driver.get("http://www.gmail.com");
}
}
While you work with Selenium ver3.x, GeckoDriver ver0.21.0 and Firefox ver61.0.1, you need to download the latest GeckoDriver from mozilla/geckodriver and store it anywhere within your system. In your code you need to provide the absolute path of the GeckoDriver through the System.setProperty() line as follows:
package demo;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class A_Firefox
{
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver", "C:/path/to/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.gmail.com");
System.out.println("Page Title is : "+driver.getTitle());
driver.quit();
}
}
Note: Replace the packagename (demo in this example) with your own package name.
While running the Selenium WebDriver code, I get the following error message.
Microsoft Windows Malicious Software Removal Tool - ... wants to restore your Chrome settings to their original defaults
This is the code I am using.
package snaptrude;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class MyProject {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
System.setProperty("webdriver.chrome.driver","C:\\Users\\Kunal\\Desktop\\Selenium\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String baseUrl = "http://demo.guru99.com/test/newtours/";
String expectedTitle = "Welcome: Mercury Tours";
String actualTitle = "";
// launch Fire fox and direct it to the Base URL
driver.get(baseUrl);
// get the actual value of the title
actualTitle = driver.getTitle();
/*
* compare the actual title of the page with the expected one and print
* the result as "Passed" or "Failed"
*/
if (actualTitle.contentEquals(expectedTitle)){
System.out.println("Test Passed!");
} else {
System.out.println("Test Failed");
}
//close Fire fox
driver.close();
}
}
I intend to perform some tests by using selenium with multiple web browsers. To distinguish between the different web drivers, I use the following line of code:
((RemoteWebDriver) driver).getCapabilities().getBrowserName();
This will return a String indicating the web browser that is used by the driver object. However, for my Opera WebDriver object this will give me the String 'chrome'. I have tried changing this by explicitly setting the browser name to 'opera' using DesiredCapabilities:
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("opera");
WebDriver driver = new OperaDriver(capabilities);
Unfortunately, this does not fix my problem. How do I effectively change the web browser name?
Your basic requirement is to identify browser initialization if I'm right, which can be done by getting user-agent from your browser using JavascriptExecutor as following:
String userAgent = (String) ((JavascriptExecutor) driver).executeScript("return navigator.userAgent;");
//following is for identifying opera browser initialization
if(userAgent.contains("OPR/"){
System.out.println("Browser currently in use is Opera");
}
Similarly you can identify other browser initilisation by referring this link
Unfortunately , you would not be able to change the BrowserName.
What instead you can try is to create function for specifically handling multiple browsers: -
package multiBrowser;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.opera.OperaDriver;
import org.testng.annotations.Parameters;
public class MultiBrowserClass {
WebDriver driver;
#Test
#Parameters("browser")
public void multiBrowsers(String browserName) throws InterruptedException{
if(browserName.equalsIgnoreCase("firefox")){
System.setProperty("webdriver.firefox.marionette","D:\\My Work\\Setup\\JAR\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("default");
driver = new FirefoxDriver(myprofile);
}
if(browserName.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver", "D:\\My Work\\Setup\\JAR\\driver\\chromedriver.exe");
driver = new ChromeDriver();
}
else if(browserName.equalsIgnoreCase("IE")){
System.setProperty("webdriver.ie.driver", "D:\\My Work\\Setup\\JAR\\driver\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
}
else if(browserName.equalsIgnoreCase("opera")){
System.setProperty("webdriver.opera.driver", "D:\\My Work\\Setup\\JAR\\driver\\operadriver.exe");
driver = new OperaDriver();
}
driver.manage().window().maximize();
driver.navigate().to("https://");
System.out.println(driver.getTitle());
driver.findElement(By.xpath("//div[#id='navbar-main']/ul/li[5]/a")).click();
driver.findElement(By.xpath("//div[#id='navbar-main']/ul/li[5]/ul/li/a")).click();
Thread.sleep(3000);
driver.findElement(By.name("email")).clear();
driver.findElement(By.name("email")).sendKeys("abc#mm.kk");
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys("1qaz2wsx");
Thread.sleep(3000);
driver.findElement(By.xpath("//form[#id='loginform']/div[8]/button")).click();
Thread.sleep(5000);
if(driver.getPageSource().contains("Welcome abc#mm.kk")){
System.out.println("User Successfully logged in");
}else{
System.out.println("Username or password you entered is incorrect");
}
driver.quit();
}
}
=======