I don't want to use action class as its failing for some browser. therefore I am using JS but I ended up with " arguments[0].hover is not a function" error for using arguments[0].hover();

WebElement ele = browser.driver.findElement(By.xpath(".//a[contains(text(),'Basic example')]"))
    Actions builder = new Actions(browser.driver)
 ((JavascriptExecutor) browser.driver).executeScript("arguments[0].hover();", ele);
    WebDriverWait wait = new WebDriverWait(browser.driver, 60);
    WebElement selenium =wait.until(ExpectedConditions.visibilityOf(ele));
    ((JavascriptExecutor) browser.driver).executeScript("arguments[0].scrollIntoView();", ele);
    waitFor(WAIT_TIME){$(By.xpath(".//a[contains(text(),'Basic example')]")).css("text-decoration-line") == "underline"}
    assert $(By.xpath(".//a[contains(text(),'Basic example')]")).css("text-decoration-line") == "underline": "No underline is displaying after hover mouse"

Url tested on: https://developer.mozilla.org/en-US/docs/Web/CSS/:hover

2 Answers 2



click() is an action but hover() is a event , so calling hover over the element doesn't do anything . It is used define what should happen when user hovers over the element.

Using action class is the best way , but still if you want to use javascript use:


Note: in the example the link is inside iframe you should switch to that first

  • Using arguments[0].focus() it will only focus on the path given but basically, I want to move the mouse on the element given as I need to validate the colour interaction post mouse on so arguments[0].focus() didn't help much.
    – meado
    Commented Feb 14, 2021 at 22:11
  • why you don't want to use actions ?
    – PDHide
    Commented Feb 14, 2021 at 22:11
  • action class is passing with chrome, firefox browsers but not with Safari & IE, its throwing MoveTargetOutOfBoundsException error .
    – meado
    Commented Feb 14, 2021 at 22:21
  • So it's passing for all safari browser capability except Catalina safari 13.1 throwing an error MoveTargetOutOfBoundsException
    – meado
    Commented Feb 15, 2021 at 0:48
  • please add your code how are you moving to that element
    – PDHide
    Commented Feb 15, 2021 at 1:03

In every testing company, automation testing services teams may face this type issue related to focusing a webElement. This can be done by doing mousehover actions by selenium or by java script.

Below is one way of getting the focus on webElement:

C#: ((IJavaScriptExecutor)webDriver).ExecuteScript("arguments[0].scrollIntoView(true);", webElement);

Java: ((JavaScriptExecutor)webDriver).executeScript("arguments[0].scrollIntoView(true);", webElement);

Here webElement is the element on which we are bringing the focus and can be passed as a variable like above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.