I am not sure how to solve this null pointer exception. My thought was the page properties are not found as the page gets loaded which is causing this. If some one could kindly point out that would be helpful. Thanks in advance.
Code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Flight {
public static WebDriver driver;
//This following section is for browser and getting the url
public static WebDriver browser(){
System.setProperty("webdriver.chrome.driver", "C:\\Users\\chq-sheikhr\\Downloads\\eclipse\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("https://www.orbitz.com/Flights");
return driver;
}
//this following section is getting the properties of the page
public static void getPageProperties(String ff,String ft, String fd, String rd){
//Flight f= new Flight(); -- I thought I was getting null pointer because properties were not found
//f.browser(); -- putting them here is how these webelements would be found and null pointer issue will be solved but NO
WebElement flyFrom= driver.findElement(By.id("flight-origin"));
WebElement flyTo= driver.findElement(By.id("flight-destination"));
WebElement flyDate= driver.findElement(By.id("flight-departing"));
WebElement returnDate= driver.findElement(By.id("flight-returnin"));
WebElement flight_search_btn= driver.findElement(By.id("search-button"));
flyFrom.sendKeys(ff);
flyTo.sendKeys(ft);
flyDate.sendKeys(fd);
returnDate.sendKeys(rd);
flight_search_btn.click();
}
// this following section will have the arguments that we will provide for flight search
public static void main (String [] args){
Flight f= new Flight();
f.browser();
f.getPageProperties("MSP", "SEA", "05/01/2017", "05/05/2017");
}
}
Error:
Only local connections are allowed.
Exception in thread "main" java.lang.NullPointerException
at Flight.getPageProperties(Flight.java:27)
at Flight.main(Flight.java:47)
You have to change
WebDriver driver= new ChromeDriver();
to
driver= new ChromeDriver();
Related
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.
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();
}
Trying to test/learn selenium to login
the error - Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: element not visible
package com.indeed.tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class test1 {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\****\\Desktop\\neww\\trainingfiles\\chromedriver.exe.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.neopets.com/login/index.phtml");
driver.findElement(By.name("username")).sendKeys("test1");
}
private static void sleep(int i) {
}
}
I had a look at that web page. The problem is that there are two input fields with the name "username". One of them is not visible. Probably Selenium is getting that one. What you should do is:
List<WebElement> elements = driver.findElements(...);
and then get the second one (or the first, whatever), then try:
elements.get(1).sendKeys(...);
I tried to input phone numbers in the field but it gives me an error
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element
Here is the code:
driver.get("https://marswebtdc.tdc.vzwcorp.com/cdl/lte/fdr_llc/fdr.jsp?3gOr4g=4g");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[#id='content column']/table[1]/tbody/tr/td/form/b/table/tbody/tr/td/table/tbody/tr[2]/td[4]/input")).click();
driver.findElement(By.xpath("//*[#id='content column']/table[1]/tbody/tr/td/form/b/table/tbody/tr/td/table/tbody/tr[2]/td[4]/input")).clear();
driver.findElement(By.xpath("//*[#id='content column']/table[1]/tbody/tr/td/form/b/table/tbody/tr/td/table/tbody/tr[2]/td[4]/input")).sendKeys("9083071303");
this is internal site you cant load the page.
I assume the sendkey() doesnt work for this field. is there any element i can use instead sendkey().
Exception org.openqa.selenium.NoSuchElementException tells the element not present on page when action is performed.
This may be because of either XPATH is not correct or element is not appeared on page before action is called.
Please run code in debug mode to find exact problem.
Here is quick example of google search box. I have put wrong id to make code fail. In this case I get the exception. If we correct the id "gbqfq" the code works fine.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class GoogleSearchUsingSelenium {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
try
{
WebElement searchBox = driver.findElement(By.id("gbqfq1")); //Incorrect ID here
searchBox.sendKeys("Cheese");
}
catch(Exception e){
System.out.println("Eelemnt Not Found : "+e.getMessage());
}
driver.close();
}
}
I have opened different browser instances and at the end i would like to close all the instances but when i use driver.close() or driver.quit() it is only closing the last instance of the browser. Please help.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class showClose {
static WebDriver driver;
public showClose(WebDriver driver){
this.driver=driver;
}
public static void main(String[] args) {
showClose sc = new showClose(driver);
sc.IE("http://www.msn.com");
sc.Firefox("http://seleniumhq.org");
sc.Chrome("http://google.com");
driver.quit();
}
//Internet Explorer driver
public void IE(String URL){
//Set the driver property for IE
System.setProperty("webdriver.ie.driver", System.getProperty("user.dir")+"\\IEDriverServer.exe");
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
//Create object of Internet explorer driver
driver = new InternetExplorerDriver(ieCapabilities);
driver.get(URL);
}
//Firefox driver
public void Firefox(String URL){
driver = new FirefoxDriver();
driver.get(URL);
}
//Chrome driver
public void Chrome(String URL){
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\chromedriver.exe");
driver = new ChromeDriver();
driver.get(URL);
}
}
In every call of „sc.IE“, „sc.Firefox“ or „sc.Chrome“ you are overwriting the instance variable “driver”.
So the only driver that is closed by your call “driver.quit” is the last one.
If you want to close the browser after visiting the URL you would either have to do a “driver.quit” in before each call to „sc.IE“, „sc.Firefox“ or „sc.Chrome“ or manage a list of WebDrivers and close all of them.
For example you could do something like this:
import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class ShowClose {
private List<WebDriver> drivers;
public ShowClose(){
this.drivers = new ArrayList<WebDriver>();
}
public static void main(String[] args) {
ShowClose sc = new ShowClose();
sc.IE("http://www.msn.com");
sc.Firefox("http://seleniumhq.org");
sc.Chrome("http://google.com");
sc.CloseAll();
}
public void CloseAll() {
for(WebDriver d : drivers) {
d.quit();
}
}
//Internet Explorer driver
public void IE(String URL){
//Set the driver property for IE
System.setProperty("webdriver.ie.driver", System.getProperty("user.dir")+"\\IEDriverServer.exe");
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
//Create object of Internet explorer driver
WebDriver driver = new InternetExplorerDriver(ieCapabilities);
driver.get(URL);
this.drivers.add(driver);
}
//Firefox driver
public void Firefox(String URL){
WebDriver driver = new FirefoxDriver();
driver.get(URL);
this.drivers.add(driver);
}
//Chrome driver
public void Chrome(String URL){
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get(URL);
this.drivers.add(driver);
}
}
Step1:
In Main Class declare 'List list' Interface and declare it as 'Static'
public static List<WebDriver> drivers;
Reason for Using List: It represents an ordered list of objects, meaning you can access the elements of a List in a specific order, and by an index too. You can also add the same element more than once to a List.
Step2:
Now Create a Constructor in which we will point to out current driver from the Stored List of Drivers. (I assume my class name as Test)
public Test()
{
this.drivers = new ArrayList<WebDriver>();
}
Step3:
Add a WebDriver Instance to out ArrayList for drivers in all methods of IE, Firefox and Chrome.
this.drivers.add(driver);
Step4: In main class copy all the instances of stored drivers to an object and use that object to close all opened instances.
for(WebDriver d : drivers)
{
d.quit();
}
For one thing: driver.quit() should be used if you want to close all windows. driver.close() is for a single window.
Perhaps it has something to do with the browser windows throwing an alert when they attempt to close?
See this other topic on StackOverflow for a solution to that problem