WebDriver class and ChromeDriver class couldn't be imported - java

I have added all the files in classpath of library. I am able to see org.openqa.selenium.chrome/ChromeDriver class and org.openqa.selenium/WebDriver class. Still Eclipse is not able to locate the class files. Below is my code.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver=new ChromeDriver();
}
}
Kindly help to resolve. Thanks.

Well, I think this is not a simple "import" problem. First you need to set the path for your chrome driver using System.setProperty, just like the example below:
#Before
public void BeforeTest() {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/driver/chromedriver"); //On windows ...chromedriver.exe
driver = new ChromeDriver();
driver.get("https://www.amazon.com.br");
}
If you are really having an import problem, maybe something is missing on the classpath. If this is the case I recommend you to use Maven and download the dependencies.

Related

The driver executable must exist

import org.openqa.selenium.WebDriver;
import org.openqa selenium.chrome.ChromeDriver;
public class SeliniumTest {
public static void main(String [] args) {
System.setProperty("webdriver.chrome.driver", chromedriver.exe");
WebDriver driver = new Chrome driver();
driver.get("https://www.google.com");
enter code here
}
}
When I run this, I get an illegal Exception:
the driver executable must exist: c:\TestProject\drivers\chromedriver.exe
I tried multiple times to install the IDE and download the right version of drivers on both Mac and Windows, but I get the same error. Can someone please help?

updated correct chrome driver path/driver version and add selenium jar too, but still getting below issue , could you help me for this?

code :
package Demo1;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Chrome {
public static void main(String[] args) {
WebDriver driver= new ChromeDriver();
System.setProperty("webdriver.chrome.driver","C:\\New folder\\chromedriver.exe");
driver.get("https://www.youtube.com/watch?v=BtmeQOcdIKI");
System.out.println(driver.getTitle());
}
}
error :
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:847)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:134)
at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:35)
at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:159)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:355)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:94)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
at Demo1.Chrome.main(Chrome.java:9)
You are setting the system property too late.
Looking at the stacktrace, the exception is being thrown while executing the following line of your code:
WebDriver driver= new ChromeDriver();
At that point, the line of your code that sets the system property hasn't been reached yet.
Evidently, you need to set the system property before creating the ChromeDriver object.
The first line in your main method should be :
System.setProperty("webdriver.chrome.driver","C:\\New folder\\chromedriver.exe");
something like this.
public class Chrome {
WebDriver driver = null;
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\New folder\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.youtube.com/watch?v=BtmeQOcdIKI");
System.out.println(driver.getTitle());
}
}
should get the job done.

Java selenium how to open a link from google search results?

I am new to automation testing in Selenium and i am doing some basic automation testing such as searching for something in Google and then clicking on a link which is required from the search results.
The code below, which i have produced works up until i get to the testing method. I am unable to select a link from the Google search page but i am not being shown any errors on my console. So i setup a thread on this particular line and it mentioned it could find the link name however the link name is used in the html code as i have checked on Google inspect.
Am i missing something obvious? I am relatively new to Selenium so any help is appreciated. Also i have tried mirroring some code from this users response "How to click a link by text in Selenium web driver java" but no luck!
Thanks
package com.demo.testcases;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class MyFirstTestScript {
private static WebDriver driver;
public static void main (String[] args) {
SetUp();
testing();
}
// TODO Auto-generated method stub
#setup
public static void SetUp () {
driver = new FirefoxDriver();
driver.get("http://www.google.co.uk");
System.setProperty("webdriver.gecko.driver", "usr/local/bin/geckodriver");
driver.findElement(By.name("q")).sendKeys("BBC" + Keys.ENTER);
}
#Test
public static void testing() {
driver.findElement(By.partialLinkText("BBC - Home")).click();
}
}
Once you obtain the search results for the text BBC on Google Home Page next to click() on the link containing the text BBC - Home you can use the following code block :
List <WebElement> my_list = driver.findElements(By.xpath("//div[#id='rso']//div[#class='rc']/h3[#class='r']/a"));
for (WebElement item:my_list)
{
if(item.getAttribute("innerHTML").contains("BBC - Home"))
item.click();
}
You can use this code:
public class MyFirstTestScript {
private static WebDriver driver;
private static WebDriverWait wait;
public static void main (String[] args) {
SetUp();
testing();
}
#setup
public static void SetUp () {
System.setProperty("webdriver.gecko.driver", "usr/local/bin/geckodriver");
driver = new FirefoxDriver();
wait = new WebDriverWait(driver,50);
driver.manage().window().maximize();
driver.get("http://www.google.co.uk");
wait.until(ExpectedConditions.elementToBeClickable(By.name("q")));
driver.findElement(By.name("q")).sendKeys("BBC" + Keys.ENTER);
}
#Test
public static void testing(){
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.linkText("BBC - Homepage"))));
driver.findElement(By.linkText("BBC - Homepage")).click();
}

Error "BeanShell 2.0b4 - by Pat Niemeyer (pat#pat.net) bsh%" while running code via eclipse

on running the below selenium code, i am getting an error in a popup:
Eclipse version:3.4.2, Selenium stand alone server used:2.51.0
I had the selenium server added to my project. Not sure why this error is popping up
package upointPckg;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Upoint {
public static void main(String[] args){
String url = "www.google.com";
WebDriver Driver = new FirefoxDriver();
Driver.manage().window().maximize();
Driver.get(url);
}
}
Also tried running it without importing the drivers, it shows the same error:
package uPointPackage;
public class UPointClass {
public static void main(String[] args) {
String url = "www.google.com";
System.out.println(url);
}
}
BeanShell 2.0b4 - by Pat Niemeyer (pat#pat.net)
Hi Guys, I was also getting same error because i had mentioned main() method within multiple classes under same package. So while executing current class from run icon at header in eclipse, compiler was getting confused to which main() method need to be executed.
So solution is to write main() method only in single class within a package or execute only single class by right click on that particular class and run as Java Application,

creating user defined function for selenium webdriver

I want to create some user defined functions for my webdriver automation code. I tried it, but resulted in failure.
the following is my code
public class snapdeal {
WebDriver driver= new FirefoxDriver();
#Test
public void test() {
// I want open browser in function 1
driver.get("http://amazon.in");
driver.manage().window().maximize();
// Function 2 for searching
driver.findElement(By.xpath("//li[#id='nav_cat_2'")).click();
driver.findElement(By.id("twotabsearchtextbox")).sendKeys("Shoes");
driver.findElement(By.xpath("//input[#class='nav-submit-input']")).click();
driver.findElement(By.xpath("//h2[#class='a-size-medium s-inline s-access-title a-text-normal' and contains(text(), \"Fbt Men's 8876 Casual Shoes\")]")).click();
}
}
How ca i write two functions inside the class?
You were probably trying to nest methods inside test() . It is not possible.
You can use this code below which calls the respective methods in the test(). It works as expected:
public class snapdeal {
static WebDriver driver= new FirefoxDriver();
#Test
public void test() {
//Method1 for Opening Browser.
openBrowser();
// Method2 for searching
searchElement();
}
public static void openBrowser(){
driver.get("http://amazon.in");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
public static void searchElement(){
driver.findElement(By.xpath("//li[#id='nav_cat_2']")).click();
driver.findElement(By.id("twotabsearchtextbox")).sendKeys("Shoes");
driver.findElement(By.xpath("//input[#class='nav-submit-input']")).click();
driver.findElement(By.xpath("//h2[#class='a-size-medium s-inline s-access-title a-text-normal' and contains(text(), \"Fbt Men's 8876 Casual Shoes\")]")).click();
}
}
I think this is like a Hello World for Selenium for you, you could make use defined methods in Java using Junit with the following annotations which can be found here
But as per norms we usually have a #Before method in Junit or #BeforeTest method in testng for setting up the webdriver and the url of AUT, also in your code a couple of xpaths were wrong which were causing the error, Please find below the correct working code with comments:
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.safari.SafariDriver;
public class snapdeal {
public WebDriver driver;
#Before
public void setUP()
{
// I want open browser in function 1
driver= new SafariDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://amazon.in");
driver.manage().window().maximize();
}
#Test
public void test() {
// Function 2 for searching
//driver.findElement(By.xpath("//li[#id='nav_cat_2")).click(); //element not needed
driver.findElement(By.id("twotabsearchtextbox")).sendKeys("Shoes");
driver.findElement(By.xpath("//input[#class='nav-submit-input']")).click();
driver.findElement(By.xpath("//*[#title=\"Fbt Men's 8876 Casual Shoes\"]//h2")).click();
}
}
The above code works as desired.
Creating user defined function have two different scope
1) Create function with piece of code and call that function whenever u needed it (Which is done above)
2) Second one creating a custom function wrt each controls like edit boxes , radiobutton , check boxes - etc , so by creating this functions u can make better feasible of your automation framework

Categories