Selenium Webdriver (Eclipse): importing issues using ctrl +shift +O - java

I am learning how to use Selenium Webdriver using Eclipse IDE. I am trying to import a browser (for example, Firefox), by using ctrl+shift+O, but it does not seem to work. It just says "0 imports added" at the bottom. What am I doing wrong? This is how my script looks so far (see below). I have a red squiggly underline below the word "WebDriver" and "FirefoxDriver":
public class Firefox {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver=new FirefoxDriver();
}
}

Mac:
Command + Shift + O
Windows:
Ctrl + Shift + O (<-- an 'O' not a zero)
For static import:
Ctrl+Shift+ M(Source > Add Import) can not only be used to add missing imports. It can also help with static
Second Edit
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Firefox {
public static void main(String[] args){
WebDriver driver=new FirefoxDriver();
}
}
1.Download the selenium jar file
In Eclipse > Right click on your project or Firefox class > Select Build path > Configure Build Path .. Select Libraries tab> Add External Jar> Add Selenium Java jar .
3.Click Ok
==========================
Edit Part Two
It seems now we need to manually download and set path to the driver executable for Mozilla Firefox.
Following is what you need to do:-
1.Downlaod Mozilla GeckoDriver latest version for MAC
Extract on your desired location i.e. c:\GeckoDriver\geckodriver.exe
Now you need to set system property and write following lines to initialize FireFoxDriver object:-
System.setProperty("webdriver.gecko.driver", "/Users/yourpath/Downloads/geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get("http://seleniumhq.com");
Thats it!

Related

Error on simple class in selenium webdrive

Executing simple class
Enter code here
package lesson1;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Brf {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver(); //Launches Firefox Browser with blank url
driver.get("http://www.gcrit.com/build3/admin/login.php");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin#123");
driver.findElement(By.id("tdb1")).click();
String url = driver.getCurrentUrl();
if (url.equals("http://www.gcrit.com/build3/admin/index.php")){
System.out.println("Login Successful -Passed");
}
else
{
System.out.println("Login Unsuccessful -Failed");
}
driver.close(); //Closes the Browser
}
}
Getting the error:
Error on simple class in selenium webdrive:
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:335)
You are getting this error because you haven't set the path of gecko driver in your code.
Please set the path of gecko driver before creating a new instance of Firefox driver.
System.setProperty("webdriver.gecko.driver"," Path to geckodriver");
WebDriver driver = new FirefoxDriver();
Need to add driver path, if you are using selenium jars above 3.0 , if you are using selenium jars below 3.0 your code is good to go, but i guess you are using selenium jars above 3.0
Public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver"," Path to geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.gcrit.com/build3/admin/login.php");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin#123");
driver.findElement(By.id("tdb1")).click();
String url = driver.getCurrentUrl();
if (url.equals("http://www.gcrit.com/build3/admin/index.php")){
System.out.println("Login Successful -Passed");
}
else
{
System.out.println("Login Unsuccessful -Failed");
}
driver.close(); //Closes the Browser
}
The cause of the error is that you don't have a proper setup for detection of a GeckoDriver file named "geckodriver.exe" (this prevents you from being able to open Firefox with your program). In case you don't have such a file, you can download one h̲e̲r̲e̲ (I suggest getting the latest version).
With such a file, you can carry out one of the following two procedures to rectify your problem:
Method 1
Add the path of the folder containing the "geckodriver.exe" file to your PATH environment variable value (instructions on how to modify this variable value can be seen h̲e̲r̲e̲).
Method 2
Prepend the body of your main method with the following statement (where GECKODRIVER_PATH represents the path of the "geckodriver.exe" file):
System.setProperty("webdriver.gecko.driver", "GECKODRIVER_PATH");
Set the gecko driver path which is initialise the firefox driver.

java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property

Here is my code:
package Basics;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class invokegoogle {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("Webdriver.chrome.driver", "C:\\Users\\sravani\\Desktop.exe");
WebDriver driver=new ChromeDriver();
driver.get("http://qaclickacademy.com");
}
}
Getting the following errors:
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:754)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124)
at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:32)
at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:329)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:124)
at Basics.invokegoogle.main(invokegoogle.java:12)
Any help is highly appreciated. Thanks in advance
Assuming the chromedriver.exe is stored at your desktop, you need to make a couple of changes as follows:
You need to replace the uppercase W with lower case w within Webdriver.chrome.driver.
As you are on windows system, presuming chromedriver.exe is placed in your desktop ypu need to append the WebDriver variant name along with the extension within the absolute path of the WebDriver.
Effectively the line of code will be:
System.setProperty("webdriver.chrome.driver", "C:\\Users\\sravani\\Desktop\\chromedriver.exe");
Note: webdriver.chrome.driver needs to start with lowercase letter.
Once you download chrome driver into your system, after extracting it (unzipping it) into the folder, it looks like you have directly copied the folder path "Downloads/chromedriver_win32.exe", instead using full path as below mentioned
Open the folder (chromedriver_win32.exe), then you will see "chromedriver.exe" as .exe file, and use this path instead and it looks like this
System.setProperty("webdriver.chrome.driver", C:\Downloads\chromedriver_win32\chromedriver.exe");
This will work
Add your chrome driver to java resource folder
Add the below mentioned code will work
System.setProperty("webdriver.chrome.driver", Objects.requireNonNull(getClass().getClassLoader().getResource("drivers/chromedriver.exe")).getFile() );
i also got same issue then searched so many answer and apply into my code but result showing null
then after i realized that i had declare static variable on above of class
looks like
private static WebDriver driver = new ChromeDriver();
then i rewrite code on under void main class like
WebDriver driver = new ChromeDriver();
now my code running works fine
you can also try like this or let me know so i can help you.

Browser opens, but never goes to URL using Selenium

I want to write couple of tests for a web page with Selenium and jUnit4, but I can't figure out how to make Firefox open the URL I need. Without System.setProperty(...) I'm getting 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. and browser never opens. Yet if I implement it the browser does open on a default start "new page", but the line driver = new FirefoxDriver(); and further never executes.
Below is the simplest code version of what I'm trying to achieve:
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Main {
static String URL = "http://www.google.com";
static WebDriver driver;
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
//Following code never executes
driver = new FirefoxDriver();
//I'm not sure if this is how I'm supposed to open URL, but I never had this code executed.
driver.get(URL);
driver.quit();
}
}
UPDATED:
These links were helpful to solve the problem of proper geckodriver installation.
https://github.com/mozilla/geckodriver/releases
http://learn-automation.com/use-firefox-selenium-using-geckodriver-selenium-3/
Actually you need to set geckodriver.exe path instead of firefox.exe in this
System.setProperty("webdriver.gecko.driver", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
So just replace geckodriver.exe path and try

Getting Started with JAVA and Selenium

I'm new to JAVA and Selenium, I installed both Eclipse and Selenium webdriver and I'm trying my first example (search a keyword in Google):
package testproject;
public class testclass {
public static void main(String[] args) {
// TODO Auto-generated method stub
Object driver;
//Open Home Page
((Object) driver).get("http://www.google.com");
//Enter text in search box
driver.findElement(By.name("q")).sendKeys("selenium");
Thread.sleep(1000);
//Click Search button
((By) driver).findElement(By.name("btnG")).click();
Thread.sleep(10000);
}
}
I got the following exception:
Exception in thread "main" java.lang.Error: Unresolved compilation
problems: The method get(String) is undefined for the type Object By
cannot be resolved By cannot be resolved to a type By cannot be
resolved at testproject.testclass.main(testclass.java:10) Picked up
JAVA_TOOL_OPTIONS: -agentlib:jvmhook**
you should import the selenium some class ,you can use the some methods and you ought crate a drive instance
Use org.openqa.selenium.WebDriver instead of Object. If you cannot import it, download the Selenium Standalone Server from http://docs.seleniumhq.org/download/ and add it to your external libraries.
In Eclipse you can add it with click right to your project > properties > Java Build Path > Add External Jars.
You also have to create the WebDriver Object easiest with
WebDriver driver = new FirefoxDriver();

The method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String)

I am trying to send a String to the sendkeys() method, but it is not accepting and throwing an error as
my codes follows:
package healthcare;
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.support.ui.Select;
import com.thoughtworks.selenium.Selenium;
import com.thoughtworks.selenium.webdriven.WebDriverBackedSelenium;
public class MailRegister_Webdriver {
public WebDriver driver;
public Selenium selenium;
public void openURL(){
//System.setProperty("webdriver.chrome.driver", "F:\\Library\\chromedriver.exe");
driver=new FirefoxDriver();
selenium=new WebDriverBackedSelenium(driver, "http://mail.in.com");
driver.get("http://mail.in.com");
}
public void register() throws Exception{
//driver.findElement(By.cssSelector("input.registernow")).click();
selenium.click("css=input.registernow");
Thread.sleep(3000);
driver.findElement(By.id("fname")).sendKeys("Nagesh");
selenium.select("day", "10");
selenium.select("month", "Jun");
new Select(driver.findElement(By.id("year"))).selectByVisibleText("1999");
Thread.sleep(1000);
driver.findElement(By.xpath("(//input[#name='radiousername'])[5]")).click();
Thread.sleep(2000);
driver.findElement(By.id("password")).sendKeys("nag123");
driver.findElement(By.id("repassword")).sendKeys);
driver.findElement(By.id("altemail")).sendKeys();
driver.findElement(By.id("mobileno")).sendKeys("7894561230");
driver.findElement(By.id("imageField")).click();
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
MailRegister_Webdriver m=new MailRegister_Webdriver();
m.openURL();
m.register();
}
}
Can somebody help on this, Why Sendkeys() method is not taking String values as arguments?
It has a simple solution. Change your compiler compliance level from 1.4 to 1.7.
Follow these steps in your eclipse:
Right click on your java project and select Build Path -> Click on
Configure Build Path...
In project properties window, Click/select Java Compiler at the left
panel
At the right panel, change the Compiler compliance level from 1.4 to 1.7
(Select which is higher version in your eclipse)
Lastly Click on Apply and OK
Now check your code. it will never show the same error.
element.sendKeys(new String[]{"Hello, selenium"});
My code looks like this, it's working.
There are two possible solution for this
1- Change the compiler version from old version to 1.5 or greater.
2- Change the JRE version from JRE8 to JRE7.
I have created a detailed article on this may be it will help.
http://learn-automation.com/solution-for-sendkeyscharsequence-in-selenium/
Try to click into the WebElement before you sending keys to it:
public static void login(WebDriver driver, String userName, String password) {
driver.get("loginPage.html");
Thread.sleep(3000);
driver.findElement(By.id("username")).click();
driver.findElement(By.id("username")).clear();
driver.findElement(By.id("username")).sendKeys(userName);
Thread.sleep(TestConfiguration.time);
driver.findElement(By.id("password")).click();
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys(password);
Thread.sleep(3000);
driver.findElement(By.name("login")).click();
Thread.sleep(3000);
}
You should use clear() method to clear the input field before using sendKeys().
You can try by replacing your following lines of code:
driver.findElement(By.id("password")).sendKeys("nag123");
driver.findElement(By.id("repassword")).sendKeys);
driver.findElement(By.id("altemail")).sendKeys();
driver.findElement(By.id("mobileno")).sendKeys("7894561230");
driver.findElement(By.id("imageField")).click();
to
driver.findElement(By.id("password")).sendKeys("nag123");
driver.findElement(By.id("repassword")).sendKeys("");
driver.findElement(By.id("altemail")).sendKeys("");
driver.findElement(By.id("mobileno")).sendKeys("7894561230");
driver.findElement(By.id("imageField")).click();
Depending on the version of java you need to either convert the primitive (i.e. Char) to String (look here: http://tech.deepumohan.com/2013/03/java-how-to-convert-primitive-char-to.html)
Or switch to a java version that would do it for you (see here: http://java-performance.info/changes-to-string-java-1-7-0_06/)
Set the JRE System Library again. If you use eclipse follow the steps below:
Go to project properties
Select Java Build Path at the left panel -> Select Libraries tab at the right
Click/select JRE System Library[] -> Click Edit button at the right side
Set your preferred JRE and click Finish button
Lastly click OK button from the project properties pop up window
Instead of editing you can also do by deleting and adding. The steps are:
Right-click on project » Properties » Java Build Path
Select Libraries tab
Find the JRE System Library and remove it
Click Add Library... button at right side » Add the JRE System Library
(Workspace default JRE)

Categories

Resources