title: Javascript Executor in Page Object Model
body: <p>For my automation script, written in Java with Selenium using Page Object Model, I would sometimes like to use Javascript Executor, because the default WebDriver clicks sometimes result in an exceptions of elements not being found.</p>
  
  <p>In the framework's page, where the web elements are initialized, the code looks like this:</p>
  
  <pre><code>public class MainPage {
  
      WebDriver driver;
      JavascriptExecutor executor = (JavascriptExecutor) driver;
  
  
      @FindBy(xpath = "//*[@id='main_button']/div/span")
      WebElement mainButton;
  
      @FindBy(xpath = "//*[@id='login_button']/div/span")
      WebElement loginButton;
  
      // constructor, where the elements are initialized
      public MainPage(WebDriver driver) {
          this.driver = driver;
          PageFactory.initElements(driver, this);
      }
  
      //method, for clicking mainButton with WebDriver
      public void clickMainButton() {
          WebDriverWait wait = new WebDriverWait(driver, 40);
          wait.until(ExpectedConditions.elementToBeClickable(mainButton)).click();
      }
  
      //method, for clicking mainButton with JS Executor
      public void clickLoginButton() {
          WebDriverWait wait = new WebDriverWait(driver, 40);
          executor.executeScript("arguments[0].click();", loginButton);
      }
  </code></pre>
  
  <p>At this point, when executing the script interacting with the page, when calling the clickMainButton(); method, it's executed correctly. But when calling the clickLoginButton(); method, the program will throw a null pointer exception:</p>
  
  <pre><code>java.lang.NullPointerException
  at pages.MainPage.clickLoginButton(MainPage.java:55)
  </code></pre>
  
  <p>If I create an instance of the Javascript Executor inside the clickLoginButton(); method, it works correctly.
  My question is how to implement the Javascript Executor correctly with Page Object Model, without having to create a new instance of it in every method?</p>