"How to fix 'selenium.JavascriptException: javascript error: Cannot read property 'left' of undefined' error in selenium jenkins

kumar gtg picture kumar gtg · Aug 21, 2019 · Viewed 7.3k times · Source

I am running my selenium code written in Java on Jenkins server. I used Actions class for mouse hovering. Used Chrome browser. It's working perfectly fine when I run it on my local machine. But not working when I run the same on jenkins. I am getting an error message at the moveToElement function. Error is:

org.openqa.selenium.JavascriptException: javascript error: Cannot read property 'left' of undefined

Following options tried.

1:Using actions class

Actions action = new Actions(driver);
action.moveToElement(WebElement).build.perform();

2:Using javaScript

String strJavaScript = "var element = arguments[0]; var mouseEventObj = document.createEvent('MouseEvents'); mouseEventObj.initEvent( 'mouseover', true, true ); element.dispatchEvent(mouseEventObj);";
        ((JavascriptExecutor) BrowserFunctionality.getBrowser()).executeScript(strJavaScript, linkNameElement);

Note: Used scrolltoview as well in javascript

3: Using Robot class

Point coordinates =webElement.getLocation();
         Robot robot = new Robot();
         robot.mouseMove(0,0);
        robot.mouseMove(coordinates.getX(),coordinates.getY()+120);

Tried these options with and without below chrome Option:

ChromeOptions options = new ChromeOptions();
options.setCapability(CapabilityType.HAS_NATIVE_EVENTS,false);

Hover it is not working in Jenkins. Any help would be appreciated

Answer