Ghostdriver 1.2.1 + PhantomJS 2.0 + latest Selenium Can't find variable error in Java
Injecting JavaScript to the web page or any other element is poor practice. You can find the Element using Selenium code and click on it without the use of any injection.
Wait for the page to be loaded
Find the button by CSS or Xpath expression
Wait for the element to by clickable
click on it using Selenium code only
WebDriverWait wait = new WebDriverWait(driver, timeToWait);
this.by = by;
try {
webElement lastFoundElement =wait.until(ExpectedConditions.visibilityOfElementLocated(by));
wait.until(ExpectedConditions.elementToBeClickable(lastFoundElement ));
new Actions(browser).moveToElement(element, offsetX, offsetY).click().build().perform();
}catch (Exception ex){
}