title: Java Selenium error "java.lang.IllegalArgumentException: Argument is of an illegal type: com.sun.proxy.$Proxy20"
body: <p>I have element in PageObjects class that I want to click by JavascriptExecuter in Functions class</p>
  
  <p>PageObject class:</p>
  
  <pre><code>package com.objects;
  
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.support.PageFactory;
  
  public class PageObject {
      protected WebDriver driver;
  
      public PageObject(WebDriver driver) {
          this.driver = driver;
          PageFactory.initElements(driver, this);
      }
  }
  </code></pre>
  
  <p>PageObjects class:</p>
  
  <pre><code>package com.objects;
  
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.WebElement;
  import org.openqa.selenium.support.FindBy;
  
  import java.util.List;
  
  public class PageObjects extends PageObject{
  
      public WebDriver driver;
  
      @FindBy (xpath = "//label[@class='mat-checkbox-layout']")
      public WebElement isRequiredCheckbox;
  
      public PageObjects(WebDriver driver) {
          super(driver);
      }
  }
  </code></pre>
  
  <p>Functions class:</p>
  
  <pre><code>package com.functions;
  
  import org.openqa.selenium.*;
  import java.util.List;
  
  public class Functions {
      public WebDriver driver;
  
      public WebElement jsClick(WebDriver driver, WebElement element){
          JavascriptExecutor executor = (JavascriptExecutor)driver;
          executor.executeScript("arguments[0].click();", element);
          return element;
      }
  }
  </code></pre>
  
  <p>when I'm calling <code>jsClick</code> method in main class, it returns error</p>
  
  <pre><code>functions.jsClick(driver, pageObjects.isRequiredCheckbox);
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>java.lang.IllegalArgumentException: Argument is of an illegal type: com.sun.proxy.$Proxy20
      at org.openqa.selenium.remote.internal.WebElementToJsonConverter.apply(WebElementToJsonConverter.java:84)
      at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
      at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
      at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
      at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
      at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
      at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
      at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
      at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:477)
      at com.functions.Functions.jsClick(Functions.java:52)
      at com.testDefinition.MainTest.addNewNoun(MainTest.java:300)
  </code></pre>
  
  <p>But if I declare element path in main class, it's working without error</p>
  
  <pre><code>WebElement isRequiredCheckbox = driver.findElement(By.xpath("//label[@class='mat-checkbox-layout']"));
  functions.jsClick(driver, isRequiredCheckbox);
  </code></pre>
  
  <p>But I want to use element path from PageObjects class </p>
  
  <p>Main class:</p>
  
  <pre><code>package com.testDefinition;
  
  import com.data.UserData;
  import com.functions.Functions;
  import com.objects.PageObjects;
  import io.cucumber.java.en.And;
  import io.cucumber.java.en.Given;
  import io.cucumber.java.en.Then;
  import org.junit.Assert;
  import org.openqa.selenium.By;
  import org.openqa.selenium.JavascriptExecutor;
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.WebElement;
  import org.openqa.selenium.firefox.FirefoxDriver;
  import org.openqa.selenium.support.ui.ExpectedConditions;
  import org.openqa.selenium.support.ui.WebDriverWait;
  
  import java.awt.*;
  import java.awt.event.KeyEvent;
  import java.util.List;
  
  public class MainTest {
      private PageObjects pageObjects;
      private UserData userData;
  
      private Functions functions = new Functions();
      private final WebDriver driver = new FirefoxDriver();
      private WebDriverWait wait = new WebDriverWait(driver, 20);
  
      @Given("Open website")
      public void runWebDriver() {
          System.setProperty("webdriver.geckodriver.driver", "/usr/local/bin/geckodriver");
          driver.get("website link");
          driver.manage().window().maximize();
      }
  
      @Then("Login by Admin")
      public void login() {
          pageObjects = new PageObjects(driver);
          userData = new UserData();
  
          pageObjects.inputLogin.sendKeys(userData.Admin);
          pageObjects.inputPassword.sendKeys(userData.AdminPass);
          pageObjects.submitButton.click();
      }
  
      @And("Start add new noun")
      public void addNewNoun() throws Throwable{
          wait.until(ExpectedConditions.visibilityOf(pageObjects.addNewNounBtn));
  
          List &lt;WebElement&gt; qaCodesList = pageObjects.nounQACodesList;
          List &lt;WebElement&gt; preservCodesList = pageObjects.nounPreservCodesList;
          List &lt;WebElement&gt; measuerUnitsList = pageObjects.measureUnitList;
  
          String qaCodeText = "Test";
  
          pageObjects.addNewNounBtn.click();
          pageObjects.inputNounModifName.sendKeys(functions.generateNounModifName());
  
          Thread.sleep(500);
          pageObjects.nounQACodesListBtn.click();
          Thread.sleep(500);
          functions.getAddNounListCode(qaCodesList, qaCodeText);
  
          pageObjects.nounPreservCodesListBtn.click();
          Thread.sleep(500);
          preservCodesList.get(2).click();
  
          wait.until(ExpectedConditions.visibilityOf(pageObjects.addSpecificationBtn));
          pageObjects.addSpecificationBtn.click();
          pageObjects.specificationName.sendKeys(functions.generateNounModifName());
  
          Thread.sleep(500);
          pageObjects.measureUnitsListBtn.click();
  
          wait.until(ExpectedConditions.visibilityOfAllElements(measuerUnitsList));
          measuerUnitsList.get(3).click();
  
          //WebElement isRequiredCheckbox = driver.findElement(By.xpath("//label[@class='mat-checkbox-layout']"));
          functions.jsClick(driver, pageObjects.isRequiredCheckbox);
      }
  }
  </code></pre>