Could not able to launch chrome browser using the selenium webdriver - java

import org.openqa.selenium.chrome.ChromeDriver;
public class launch {
public static void main(String[] args) {
// TODO Auto-generated method stub
ChromeDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
}
}
Error:
Could not find or load main class org.openqa.grid.selenium.GridLauncherV3

In the Launch class you are directly creating the object of ChromeDriver class without giving the chrome driver path.
So, before creating the ChromeDriver object you need to add:
System.setProperty("webdriver.chrome.driver","path of chromedriver.exe file");
If you have downloaded the chrome driver exe file then you need to pass that path.
After this create the object of ChromeDriver class.

You need to set the system property webdriver.chrome.driver as follows:
System.setProperty("webdriver.chrome.driver", '/path/to/chromedriver');
Additionally, instead of using the ChromeDriver use the WebDriver interface as follows:
WebDriver driver = new ChromeDriver();

Related

Error while executing Selenium script in Eclipse IDE

I am getting the below error for the below script. I have all the jar files added to the library.
============================================================
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
WebDriver cannot be resolved to a type
FirefoxDriver cannot be resolved to a type
at webdriver/Demo.Sample.main(Sample.java:11)
=============================================================
package Demo;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Sample {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("hello");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
}
}
Seems like, you have imported the wrong jar files. Download the selenium-server-standalone-2.53.1.jar file from https://selenium-release.storage.googleapis.com/index.html?path=2.53/ and import it into an eclipse and remove other jar files.
Since you are trying to automate Firefox using Selenium WebDriver, you need to download Firefox executable binary file as well. Based on your browser version, you can get gecko driver from https://github.com/mozilla/geckodriver/
And add the below line in your code before the driver initialization and provide absolute path of Firefox executable binary file with name and extention :
System.setProperty("webdriver.firefox.driver", "pathToGeckoBinaryExecutable");
Below is your modified code :
package Demo;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Sample {
public static void main(String[] args) {
System.setProperty("webdriver.firefox.driver", "pathToGeckoBinaryExecutable\\geckodriver.exe"); // Provide your system path to the gecko driver with name and extension here
System.out.println("hello");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
}
}
I hope it helps...

Selenium code in java is not opening the browser

I am new in selenium and trying to open https://google.co.in in the chrome browser through selenium (below is the code). But I am not able to see the chrome browser after running this code. Could someone tell me that what's wrong with this code.
Here is my code.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Test {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "E:\\Application\\chrome.exe");
System.out.println("Loading...");
WebDriver driver = new ChromeDriver();
driver.get("http://google.co/in");
String appTitle = driver.getTitle();
System.out.println("Application title is :: "+appTitle);
driver.quit();
}
}
And the output is...
Loading...
Download chromedriver from this link : http://chromedriver.storage.googleapis.com/2.24/chromedriver_win32.zip , unzip it & put chromedriver.exe in "E:\Application" and provide path to chromedriver in System.setProperty("webdriver.chrome.driver", "E:\\Application\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", "E:\\Application\\chrome.exe");
Here E:\Application\chrome.exe is not your chrome driver.
Download the chrome driver of the version you need in your application.
Latest Release: ChromeDriver 2.24
Once you have the chrome driver , specify its location via the webdriver.chrome.driver system property (see sample below)
#Test
public void testGoogleSearch() {
// Optional, if not specified, WebDriver will search your path for chromedriver.
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com/xhtml");
Thread.sleep(5000); // Let the user actually see something!
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("ChromeDriver");
searchBox.submit();
Thread.sleep(5000); // Let the user actually see something!
driver.quit();
}
You can use a following library
webdrivermanager
after using this, you don't need to download a driver for the specific browser.It will automatically download driver for you and setup.
In order to use WebDriverManager in a Maven project, first add the following dependency to your pom.xml:
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>1.4.10</version>
</dependency>
Then you can let WebDriverManager to do manage WebDriver binaries for your application/test. Take a look to this JUnit example which uses Chrome with Selenium WebDriver:
public class ChromeTest {
protected WebDriver driver;
#BeforeClass
public static void setupClass() {
ChromeDriverManager.getInstance().setup();
}
#Before
public void setupTest() {
driver = new ChromeDriver();
}
#After
public void teardown() {
if (driver != null) {
driver.quit();
}
}
#Test
public void test() {
// Using Selenium WebDriver to carry out automated web testing
}
}
Notice that simple adding ChromeDriverManager.getInstance().setup(); WebDriverManager does magic for you:
It checks the latest version of the WebDriver binary file
It downloads the binary WebDriver if it is not present in your system
It exports the required Java variable by Selenium WebDriver
So far, WebDriverManager supports Chrome, Opera, Internet Explorer, Microsoft Edge, PhantomJS, or Marionette as follows:
ChromeDriverManager.getInstance().setup();
InternetExplorerDriverManager.getInstance().setup();
OperaDriverManager.getInstance().setup();
EdgeDriverManager.getInstance().setup();
PhantomJsDriverManager.getInstance().setup();
MarionetteDriverManager.getInstance().setup();

Firefox 48 and Selenium timeout issue

I am unable to run my tests on Firefox 48 using latest Selenium versions (2.53, Selenium 3 beta).
Please explain the configuration needed and the code to use to successfully run tests on Firefox 48. I have pointed to the geckodriver and tried to initialise the same in my code.
Code:
System.setProperty("webdriver.gecko.driver","E:\\Work\\Selenium\\geckodriver-v0.9.0-win64\\geckodriver.exe");
WebDriver driver = null;
driver = new MarionetteDriver();
Getting the below:
org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
Worked for me:
System.setProperty("webdriver.gecko.driver", "PATH TO GECKO DRIVER");
DesiredCapabilities ffCapabilities = DesiredCapabilities.firefox();
ffCapabilities.setCapability("marionette",true);
WebDriver driver = new FirefoxDriver(ffCapabilities);
You need to write DesiredCapabilities. Add this line before driver initialization.
DesiredCapabilities cap = DesiredCapabilities.firefox()
You can download geckodriver from the link
https://github.com/mozilla/geckodriver/releases
Then save the file in your local system. unzip the file and change the application name as "wires.exe".
Then specify the path upto wires.exe in the code.
add selenium-2.53.0 jar files.
Try below code to start working on FF 47.0 or above.
package com.marionette.programs;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.MarionetteDriver;
public class HandleLatestFirefox {
public static void main(String[] args) {
String currentDir = System.getProperty("user.dir");
System.out.println(currentDir);
//String marionetteDriverLocation = currentDir + "G:\\ravik\\Ravi-Training\\Selenium\\Marionette for firefox\\wires.exe";
System.setProperty("webdriver.gecko.driver", "G:\\ravik\\Ravi-Training\\Selenium\\Marionette for firefox\\wires.exe");
WebDriver driver = new MarionetteDriver();
driver.get("https://www.google.co.in/webhp?hl=en&sa=X&ved=0ahUKEwjdgc21jJHOAhVCvY8KHZ4aCdcQPAgD");
System.out.println("marionette working fine....");
}
}

The path to the driver executable must be set by the webdriver.gecko.driver issue in selenium webdriver

I'm trying to run the following sample snippet
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Test
{
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
//System.out.println("My new program");
}
}
When I run this code, getting the following error.
The path to the driver executable must be set by the webdriver.gecko.driver system property;
Firefox version is 48.0
Could anyone please help me to fix this issue.
If you are using windows follow the steps:
Right click my computer and select properties.
Click advanced settings->Environment variables.
Under System variable there should be variable named Path.
By the end of the path variable value add semi colon and then add specify your jecko driver's path. Example(C:\Jeckodriver).
Now compile the code. If it still throws exception then downgrade Firefox to 47.0.1.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Test
{
public static WebDriver driver;
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver","Browser path.exe");
driver = new FirefoxDriver();
driver.get("http://www.google.com");
//System.out.println("My new program");
}
}
Download Gecko driver and extract to any folder. Specify gecko driver's path in path variable.

How to use ChromeDriver in Selenium

I'm using windows on my system. I downloaded and extracted the chromedriver.exe file and I added it to my path.
Here is my code:
package com.chrometester.webdriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class chromeTest {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Tgagn_000\\Desktop\\selenium\\chrome");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
}
}
But it comes back with an error:
Exception in thread "main" java.lang.IllegalStateException: The driver executable is a directory: C:\Users\Tgagn_000\Desktop\selenium\chrome
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Tgagn_000\\Desktop\\selenium\\chrome\\chromedriver.exe");
This should fix it. You should point to the driver file, not to its directory.
You are not adding the exe. Possibly
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Tgagn_000\\Desktop\\selenium\\chrome.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
As error says, you have given directory path and not .exe path.
C:\Users\Tgagn_000\Desktop\selenium\chrome\ chromedriver.exe
Use below:
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Tgagn_000\\Desktop\\selenium\\chrome\\chromedriver.exe");
You have to be careful about order of the code:
You have to write setProperty code first then initialize the ChromeDriver()
below code sequence will give you an error
WebDriver driver= new ChromeDriver();
System.setProperty("webdriver.chrome.driver", "C:\\driver\\chromedriver.exe");
driver.get("https://www.google.com/");
Below code will work
System.setProperty("webdriver.chrome.driver", "C:\\driver\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("https://www.google.com/");

Categories