NoClassDefFoundError error while using Selenium with java - java

I am new to Selenium and I am trying to access google.co.in. My code is
public static void main(String[] args) {
String baseUrl = "https://www.google.co.in/";
WebDriver driver = new FirefoxDriver();
driver.get(baseUrl);
}
But I am getting an error which says Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Function
and I have also written two import statements after loading the appropriate jar file
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
I am new to both selenium and java..So please help..

You must add this library:
selenium-server-standalone-version.jar
and
selenium-java-version.jar
download here: Selenium Downloads

Related

Did i miss anything when trying to get selenium working with eclipse? [duplicate]

This question already has answers here:
ChromeDriver and WebDriver for Selenium through TestNG results in 4 errors
(2 answers)
Closed 2 years ago.
I am getting the next error with this code:
package main;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class main {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new ChromeDriver("https://www.google.co.il/");
}
}
Error:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
WebDriver cannot be resolved to a type
ChromeDriver cannot be resolved to a type
at class6_v2/main.main.main(main.java:11)
i have added the Jars of selenium to the build path:
enter image description here
and checked that the web driver is the right version of chrome and in the class files
what am i doing wrong?
You need to set up System Property first or set up your chromedriver.exe in to the environment path.
System.setProperty("webdriver.chrome.driver", path to your chromedriver.exe)
then you declare chrome driver and go to url
WebDriver driver = new ChromeDriver();
driver.get(your url);
Reference
https://www.guru99.com/selenium-tutorial.html

Selenium with java issue using webdriver.firefox.marionette

package main_files;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class main_downloader {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.firefox.marionette", "C:\\Users\\ahmed\\Desktop\\JavaPackage\\packages\\webdriver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.wait(1);
driver.get("https://www.google.com/");
driver.quit();
}
}
idk why, but for some reason this not working !, when I try to run it it just opens a Firefox window and do nothing !, even the search bar "which is supposed to recognize that I'm using a bot and change it's theme" is not doing that, could anyone tell me what's the exact problem please?!, Thx
The current implementation of Selenium no more uses the key webdriver.firefox.marionette and instead of that you need to use webdriver.gecko.driver. Effectively, you need to replace the line:
System.setProperty("webdriver.firefox.marionette", "C:\\Users\\ahmed\\Desktop\\JavaPackage\\packages\\webdriver\\geckodriver.exe");
with:
System.setProperty("webdriver.gecko.driver", "C:\\Users\\ahmed\\Desktop\\JavaPackage\\packages\\webdriver\\geckodriver.exe");
References
You can find a couple of relevent detailed discussions in:
UnreachableBrowserException Caused by: java.lang.NullPointerException when “webdriver.firefox.marionette” is used
Difference between webdriver.firefox.marionette & webdriver.gecko.driver

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...

Java - Get error when trying to login to a website with selenium webdriver (htmlunit)

I tried the below code with actual credentials for Indeed.com,
and I get an error:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate a node using //*[#id="signin_email"]
I get a similar error when I use By.id instead of By.xpath, any idea what's going on?
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Testing {
public static void main(String[] args) {
WebDriver driver = new HtmlUnitDriver();
driver.get("https://secure.indeed.com/account/login?service=my&hl=en_CA&co=CA");
driver.findElement(By.xpath("//*[#id=\"signin_email\"]")).sendKeys("notworking#gmail.com");
driver.findElement(By.xpath("//*[#id=\"signin_password\"]")).sendKeys("needHelp");
driver.findElement(By.xpath("//*[#id=\"loginform\"]/button")).click();
driver.quit();
}
}
You need to enable java script, see the updated code.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Testing {
public static void main(String[] args) {
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);
driver.get("https://secure.indeed.com/account/login?service=my&hl=en_CA&co=CA");
driver.findElement(By.xpath("//*[#id=\"signin_email\"]")).sendKeys("notworking#gmail.com");
driver.findElement(By.xpath("//*[#id=\"signin_password\"]")).sendKeys("needHelp");
driver.findElement(By.xpath("//*[#id=\"loginform\"]/button")).click();
driver.quit();
}
}
The login form is added via JS not regular HTML. I tried to disable JS in the browser and all I could see was:
<div id="container"></div>
This means that all you have to do is:
Enable JavaScript for your driver: driver.setJavascriptEnabled(true);
Wait until "signin_email" is rendered: driver.until(ExpectedConditions.presenceOfElementLocated(By.id("signin_email")))
The second bullet will give you stable test on different PCs. Sometimes, on weaker machines, the assertions may execute faster than the element was rendered by JS which leads to random failures.

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.

Categories