I'm using Java,Selenium web driver and TestNG framework. I have a method to click a button in web page. But it didn't work. When debugging I could find xpath is not passed to the button. Then I tried giving xpath in the same place where click event is called using driver.findElement(By.xpath.....). It worked without any error. I have included my codes below. Please check and help me to solve this problem.


// Method body   
public void ClickNewButton() {click(NewButton);}       

@FindBy(xpath = Locators.newButton)  // In this place newButton has it's xpath
WebElement NewButton;  // Here NewButtton doesn't contain any xpath. It's null (This is where problem occurs exactly)

// Xpath in Locators class
public static final String newButton = "html/body/div[2]/div[2]/div/div[2]/div/div[1]/div[3]/div[2]/div[1]/div/div[2]/div/div[3]/div/div[2]/button";               

// click method's body in Base class
public void click(WebElement webElement) {

Please note that first three code segments are in same class and that class is extended from Base class

  • please share your html file Commented May 3, 2018 at 5:16
  • @sameer, I don't think that it's possible. But I can confirm there is nothing wrong with the xpath. I've tested it.
    – Syrus
    Commented May 3, 2018 at 5:21
  • Did you try with some wait time?
    – Sachintha
    Commented May 3, 2018 at 5:32
  • 1
    give xpath like below and try @FindBy(xpath = "your xpath") private WebElement btnSubmit;
    – Sachintha
    Commented May 3, 2018 at 5:44
  • I guess you are talking about a scenario of element is not found. If it's so , I didn't use wait but I have tried sleep method to wait until the interface loads properly. Seems to be that's not the problem
    – Syrus
    Commented May 3, 2018 at 5:45

4 Answers 4


I found the solution to the problem. I just had to add single line of code to make this work.

MyClass object= PageFactory.initElements(driver, MyClass.class);       

I had to add this code to the class where I was creating the test and tried. It worked. The Error wasn't in using of @FindBy. So I guess this will help someone in the future.


Use JavascriptExecutor -

JavascriptExecutor js = (JavascriptExecutor)driver;

js.executeScript("arguments[0].click();", yourLoginButton);
  • 3
    Why do you think this is going to solve Null Pointer Exception? The question is about solving the NPE and not how you can achieve this using other method. Commented May 7, 2018 at 6:36

I think the line which is causing the problem may be

@FindBy(xpath = Locators.newButton)

You are directly calling an element from the Locators class, right?

Try creating an object of Locators class in the Class where you are using the @FindBy.

I may not be correct because the flow that you are given in the question is not enough for finding the solution.

package org.com;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

import io.cucumber.java.After;
import io.cucumber.java.Before;

public class ASOS {
    WebDriver driver;
     public void login() {
        driver=new ChromeDriver();
       // driver.manage().timeouts().implicitlyWait(300, TimeUnit.SECONDS);
        WebElement txtUserName=driver.findElement(By.xpath("//a[@href='/deals?ref_=nav_cs_gb']"));

   public void TodayDealsPage() throws InterruptedException {
//     JavascriptExecutor executor = (JavascriptExecutor)driver;
//     executor.executeScript("window.scroll(0,450)","");
       JavascriptExecutor js1 = (JavascriptExecutor) driver;
       js1.executeScript("window.scrollBy(0, 450)",""); 
       WebElement laptop = driver.findElement(By.xpath("//div[text()='Top Offers on Laptops & Desktops; Upto INR 40000 Off']"));
  • 1
    Welcome to the community! It would be helpful to add an explanation to your code on how this solves the problem.
    – Lee Jensen
    Commented Feb 15 at 19:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.