1

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

asked Feb 11, 2021 at 4:45

2 Answers 2

1

https://www.w3schools.com/jsref/met_html_click.asp

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:

arguments[0].focus()

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

answered Feb 11, 2021 at 8:34
6
  • 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. Commented Feb 14, 2021 at 22:11
  • why you don't want to use actions ? Commented Feb 14, 2021 at 22:11
  • action class is passing with chrome, firefox browsers but not with Safari & IE, its throwing MoveTargetOutOfBoundsException error . 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 Commented Feb 15, 2021 at 0:48
  • please add your code how are you moving to that element Commented Feb 15, 2021 at 1:03
1

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.

answered Mar 15, 2021 at 5:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.