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
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);
-
3Why 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– demouser1232018年05月07日 06:36:06 +00:00Commented 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;
@Test()
public void login() {
System.setProperty("webdriver.chrome.driver","C:\\Users\\Raja\\eclipse-workspace\\FebrevisionTask\\Driver\\chromedriver.exe");
driver=new ChromeDriver();
driver.get("https://www.amazon.in/ref=nav_logo");
driver.manage().window().maximize();
// driver.manage().timeouts().implicitlyWait(300, TimeUnit.SECONDS);
WebElement txtUserName=driver.findElement(By.xpath("//a[@href='/deals?ref_=nav_cs_gb']"));
txtUserName.click();
}
@Test()
public void TodayDealsPage() throws InterruptedException {
Thread.sleep(2000);
// JavascriptExecutor executor = (JavascriptExecutor)driver;
// executor.executeScript("window.scroll(0,450)","");
JavascriptExecutor js1 = (JavascriptExecutor) driver;
js1.executeScript("window.scrollBy(0, 450)","");
Thread.sleep(3000);
WebElement laptop = driver.findElement(By.xpath("//div[text()='Top Offers on Laptops & Desktops; Upto INR 40000 Off']"));
laptop.click();
}
}
-
1Welcome to the community! It would be helpful to add an explanation to your code on how this solves the problem.Lee Jensen– Lee Jensen2024年02月15日 19:44:22 +00:00Commented Feb 15, 2024 at 19:44
Explore related questions
See similar questions with these tags.
@FindBy(xpath = "your xpath") private WebElement btnSubmit;