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();
Related
this is my class*****
```
package automation;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Test1 {
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "C:/Users/UMASHANKAR/Downloads/chromedriver_win32/chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.findElement(By.id("userName")).sendKeys("https://sdzclient-kpiregister.azurewebsites.net/");
driver.findElement(By.id("passwords")).sendKeys("Gravity#123");
driver.findElement(By.id("btn-sdz-login")).click();
}
}
```
on Hover the SendKeys method will get an error like"The method sendKeys(char sequence[] )in the type webelement is not applicable for the string".***
When you are working with Selenium you need to follow a few steps
//first you add your chrome driver path
System.setProperty("webdriver.chrome.driver", "C:/Users/UMASHANKAR/Downloads/chromedriver_win32/chromedriver.exe");
// second you need to initialize the WebDriver object - and you did it
WebDriver driver=new ChromeDriver();
// third you need to tell to the WebDriver object where to go, what page to load
driver.get("https://sdzclient-kpiregister.azurewebsites.net/");
//below is the login part
driver.findElement(By.id("userName")).sendKeys("SET_YOUR_USERNAME_HERE");
driver.findElement(By.id("passwords")).sendKeys("Gravity#123");
driver.findElement(By.id("btn-sdz-login")).click();
your error was throw because the driver didn't know where to go but you tried to send some keys instead of a driving page path
I am not sure, when you said you are not able to change compilation version. You can change as per below screen grab.
..project>right click>build path>configure build path >java compiler>
Do not forget to click on Apply after changing compilation version.
i am trying this code but it fails
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Fijan {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "C:Users\\Devrepublic\\Documents\\fijan\\selenium\\chromedriver_win32\\chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("");
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from ChromeDriver to WebDriver
at prac.Fijan.main(Fijan.java:12)
add the following jars to buildpath - selenium-java.jar, selenium-remote-driver.jar, selenium-support.jar
Eclipse:
How to import a jar in Eclipse
InteliJ
Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA project
I have a maven-powered Robot-framework project in java that uses selenium 3.4.0 dependency, robotframework 3.0.2 dependency, markusbernhardt's selenium2library version 1.4.0.8, and robotframework-maven-plugin version 1.4.7.
My robot tests live in the src/main/test/robotframework/acceptance folder, while in src/main/java/mypackage I created a Customized.java file to set a system property for the browser driver path (I then import this library in my tests:
*** Settings ***
Library Selenium2Library
Library mypackage.Customized
This works perfectly. But now I'd like to implement my own keywords to extend Selenium2Library. But I'm not sure how to obtain the current WebDriver instance that is running.
I mean, if I weren't using Robot and just plain Selenium, I'd do something like this:
WebDriver driver=new ChromeDriver();
driver.doSomething();
However, in this case I don't want to instantiate a new WebDriver but instead get the one that is currently running (which Robot automatically instantiated). How can I do that?
I've so far created a Selenium2Library object and set it with the value returned by Selenium2Library.getLibraryInstance(); but that's not giving me access to selenium's methods (e.g.: getCurrentUrl() is not listed).
in python it can be done with following code
from robot.libraries.BuiltIn import BuiltIn
def _browser(self):
return BuiltIn().get_library_instance('Selenium2Library')._current_browser()
Actually, I found a solution, but I'm not sure it's the right approach:
public class Customized {
private static Selenium2Library s;
private BrowserManagement b;
private WebDriver driver;
public Customized() throws ScriptException {
try {
Customized.s = (Selenium2Library) Selenium2Library.getLibraryInstance();
} catch (javax.script.ScriptException e) {
e.printStackTrace();
}
b = s.getBrowserManagement();
driver=b.getCurrentWebDriver();
}
}
Now the selenium methods are available from the driver object. However, there might be a better way to do it, as there's this message in the javax.script.ScriptException exception: "Access restriction: The type 'ScriptException' is not API (restriction on required library 'C:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar')"
For newer Selenium version (4.0) the following snippet can be used:
class SelCustomLib():
def get_browser_driver(self):
return BuiltIn().get_library_instance('SeleniumLibrary').driver
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!
I tried to implemented a web service from a dynamic web project. I added the selenium-server-standalone-2.32.0.jar file to the buildpath, then I also added it to the WEB-INF/lib folder. Then I used the web service wizard to generate the web service from the project. At the start of the wizard it displayed a pop-up warning that read:
The service class "test.eko3.TestEko3" does not comply to one or more requirements of the JAX-RPC 1.1 specification, and may not deploy or function correctly.
The value type "org.openqa.selenium.WebDriver" used via the service class "test.eko3.TestEko3" does not have a public default constructor. Chapter 5.4 of the JAX-RPC 1.1 specification requires a value type to have a public default constructor, otherwise a JAX-RPC 1.1 compliant Web service engine may be unable to construct an instance of the value type during deserialization.
The field or property "windowHandles" on the value type "org.openqa.selenium.WebDriver" used via the service class "test.eko3.TestEko3" has a data type, "java.util.Set", that is not supported by the JAX-RPC 1.1 specification. Instances of the type may not serialize or deserialize correctly. Loss of data or complete failure of the Web service may result.
I clicked ok and it generated the web server and the client and displayed the client in the eclipse's browser. But when I entered the parameters and clicked invoke it displayed this exception in the result section:
Exception: java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver; nested exception is: java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver Message: java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver; nested exception is: java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
Since I added the selenium jar into both the buildpath and the WEB-INF/lib folder I'm not sure why it can't find the class. The code for the server is below:
package test.eko3;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
public class TestEko3 {
public String Ekobilet(String from, String to, String date) {
//Firefox browser instantiation
WebDriver driver = new FirefoxDriver();
//Loading the URL
driver.get("http://www.amadeusepower.com/trek/portals/trek/default.aspx?Culture=en-US");
WebElement radioOneway = driver.findElement(By.id("ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_rbFlightType_1"));
radioOneway.click();
waitForPageLoaded(driver);
WebElement fromText = driver.findElement(By.id("ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_txtSearch_txtFrom"));
fromText.clear();
fromText.sendKeys(from);
WebElement toText = driver.findElement(By.id("ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_txtSearch_txtTo"));
toText.sendKeys(to);
WebElement dateText = driver.findElement(By.id("ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_txtDepartureDate_txtDate"));
dateText.sendKeys(date);
//Sign in button identification and click it
WebElement searchbutton = driver.findElement(By.id("ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_btnSearch"));
searchbutton.click();
String page = driver.getPageSource();
// Writer out = new BufferedWriter(new OutputStreamWriter(
// new FileOutputStream("ekobiletselenium.html"), "UTF-8"));
// try {
// out.write(page);
// } finally {
// out.close();
// }
//Closing the browser
driver.close();
return page;
}
public static void waitForPageLoaded(WebDriver driver) {
ExpectedCondition<Boolean> expectation = new
ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
}
};
Wait<WebDriver> wait = new WebDriverWait(driver,30);
try {
wait.until(expectation);
} catch(Throwable error) {
System.out.println("exception yavrum");
}
}
}
Can someone please tell me the cause of this? Am I missing a jar file that selenium depends on? Any help would be appreciated.
Maybe you are using the standalone jar which is not good for web projects.
See http://me-ol-blog.blogspot.co.il/2013/07/using-selenium-in-java-dynamic-web.html
This exception appears when selenium is not fully referenced in the project.
Here are the steps you can take to resolve the issue:
Download the Selenium Client & WebDriver Java Bindings here.
Unzip the downloaded file.
Copy the main selenium '.jar' (example: selenium-java-2.42.2.jar) to the web project's WebContent/WEB-INF/lib dir.
Also copy All the .jar files the Unzipped selenium libs dir to the web project's WebContent/WEB-INF/lib dir.
You should now be able to run the code without getting the java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver exception.
Please check this link definitely you can get the idea which is answered by #thedrs
http://me-ol-blog.blogspot.co.il/2013/07/using-selenium-in-java-dynamic-web.html