0
package PageFactory;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.FindBy;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import org.openqa.selenium.support.PageFactory;
    import org.openqa.selenium.By;


    public class Add{
        public static WebDriver driver;
        public static void main (String args[]){
        try
        {
        WebElement popup=driver.findElement(By.xpath("/html/body/div/div[2]/a[1]"));
        WebElement name=driver.findElement(By.xpath("//*[@id='name']"));
        WebElement submit=driver.findElement(By.xpath("//*[@id='submit']"));
        WebElementproceed=driver.findElement(By.xpath("/html/body/div/div[2]/a[2]"));


            System.setProperty("webdriver.chrome.driver","E:\\Chrome\\chromedriver.exe");
            driver=new ChromeDriver();

            driver.navigate().to("testurl");    
            popup.click();  
            String oldTab = driver.getWindowHandle();
            ArrayList<String> newTab = new ArrayList<String>(driver.getWindowHandles());
            newTab.remove(oldTab);
            driver.switchTo().window(newTab.get(0));
            name.sendKeys("neha");
            submit.click();
            driver.close();
            driver.switchTo().window(oldTab);
            proceed.click();
        }catch (Exception e) {
            e.printStackTrace();
        }

        }



    }

I am trying on selenium webdriver

I tried a lot to resolve this but no success java 1.8 chrome 67 webdriver selenium server standalone-3.12.0.jar

tried with tesng then also getting the same error

1 Answer 1

1

Before creating the driver initialization, you have find the element and hence null pointer exception is throwing . Please change the code as below by moving the driver initialization part above the webelement

        System.setProperty("webdriver.chrome.driver","E:\\Chrome\\chromedriver.exe");
        driver=new ChromeDriver();

        WebElement popup=driver.findElement(By.xpath("/html/body/div/div[2]/a[1]"));
        WebElement name=driver.findElement(By.xpath("//*[@id='name']"));
        WebElement submit=driver.findElement(By.xpath("//*[@id='submit']"));
        WebElementproceed=driver.findElement(By.xpath("/html/body/div/div[2]/a[2]"));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.