1

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.

ClickNewButton();      

// 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) {
    webElement.click();
}

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

  • please share your html file – sameer joshi May 3 '18 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. – Joe May 3 '18 at 5:21
  • Did you try with some wait time? – Sachintha May 3 '18 at 5:32
  • 1
    give xpath like below and try @FindBy(xpath = "your xpath") private WebElement btnSubmit; – Sachintha May 3 '18 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 – Joe May 3 '18 at 5:45
1

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.

0

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. – demouser123 May 7 '18 at 6:36
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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